Python爬虫 | 批量爬取今日头条街拍美图
Python爬虫与数据挖掘
共 6340字,需浏览 13分钟
·
2021-07-06 10:08
点击上方“Python爬虫与数据挖掘”,进行关注 回复“书籍”即可获赠Python从入门到进阶共10本电子书
浮云一别后,流水十年间。
01
前言
02
网页分析
03
爬虫实战
def get_page(page_num):
global headers
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
}
params = {
'keyword':urllib.parse.unquote('%E8%A1%97%E6%8B%8D'),
'pd':'atlas',
'dvpf':'pc',
'aid':4916,
'page_num':page_num,
'search_json':'%7B%22from_search_id%22%3A%22202106100003510102121720341003A4ED%22%2C%22origin_keyword%22%3A%22%E8%A1%97%E6%8B%8D%22%2C%22image_keyword%22%3A%22%E8%A1%97%E6%8B%8D%22%7D',
'rawJSON': 1,
'search_id':'202106100004290101500200495C05B763'
}
url='https://so.toutiao.com/search?'+urlencode(params)
try:
response=requests.get(url,headers=headers,params=params)
if response.status_code==200:
return response.json()
except requests.ConnectionError:
return None
def get_images(json):
images=json.get('rawData').get('data')
for image in images:
link = image.get('img_url')
yield link
def saving_img(link):
global name
print(f'-------正在打印第{name}张图片')
data=requests.get(link,headers=headers).content
with open(f'image1/{name}.jpg','wb')as f:
f.write(data)
name+=1
def main(paga_num):
json=get_page(paga_num)
for link in get_images(json):
saving_img(link)
if __name__ == '__main__':
for i in range(0,2):
main(i)
04
小结
------------------- End -------------------
往期精彩文章推荐:
欢迎大家点赞,留言,转发,转载,感谢大家的相伴与支持
想加入Python学习群请在后台回复【入群】
万水千山总是情,点个【在看】行不行
/今日留言主题/
随便说一两句吧!
评论