女生最爱自拍,那就告诉她如何拍照最美,每天给她变美秘籍,python代码写起来!
importre
frombs4 importBeautifulSoup
frompandas importjson
fromurllib.parse importurlencode
fromrequests.exceptions importRequestException
importrequests
defget_index_page(offset,keyword):
data={
'offset': offset,
'format': 'json',
'keyword': keyword,
'autoload': 'true',
'count': '20',
'cur_tab': 1
}
url = 'http://www.toutiao.com/search_content/?'+urlencode(data)
try:
response = requests.get(url)
ifresponse.status_code == 200:
returnresponse.text
return None
exceptException:
print('请求索引页出错')
defparse_index_page(html):
data = json.loads(html)
ifdata and'data' indata.keys():
foritem indata.get('data'):
yielditem.get('article_url')
defget_detail_page(url):
try:
response = requests.get(url)
ifresponse.status_code == 200:
returnresponse.text
return None
exceptException:
print('请求详情页出错',url)
defparse_detail_page(html,url):
soup = BeautifulSoup(html,'lxml')
title = soup.select('title')[0].get_text()
print(title)
images_patten = re.compile('var gallery = (.*?);',re.S)
result = re.search(images_patten,html)
ifresult:
data = json.loads(result.group(1))
ifdata and'sub_images' indata.keys():
sub_images = data.get('sub_images')
images = [item.get('url') foritem insub_images]
return{
'title':title,
'url':url,
'images':images,
}
defmain():
html = get_index_page(0,'街拍')
forurl inparse_index_page(html):
html = get_detail_page(url)
ifhtml:
result = parse_detail_page(html,url)
print(result)
if__name__ == '__main__':
main()
以上代码输出图集的名称,图集的地址以及每一张图片的url,这样就能每天都能推送你心爱的女生哦~同时可以安全的炫技哦!还不赶紧去试试!
希望大家和我一起交流哦~如何快速突破python3呢?加python的Q群583677663 |
|