python爬虫--爬取微博图片

python爬取胡歌微博图片
详细教程如下:
python爬虫--爬取微博图片_第1张图片
F12或右键–审查元素:
python爬虫--爬取微博图片_第2张图片
点击network,刷新:
python爬虫--爬取微博图片_第3张图片
点击preview,可以看出数据为json格式:
python爬虫--爬取微博图片_第4张图片
查看图片链接:
python爬虫--爬取微博图片_第5张图片
python爬虫--爬取微博图片_第6张图片
可知:图片链接为–http://wx2.sinaimg.cn/sq612/48e837eely1g0ahle8hk4j21o01lnx6p.jpg
python爬虫--爬取微博图片_第7张图片
python爬虫--爬取微博图片_第8张图片
从以上图片分析可知,图片链接为:pic_host+/sq612/+pic_name
详细程序:

#导入所需库
import requests
#图片链接
url = 'http://photo.weibo.com/photos/get_all?uid=1223178222&album_id=3562507013282779&count=30&page=1&type=3&__rnd=1550915606445'
#headers作用为反爬
headers = {
        'Cookie':'login_sid_t=3e61e90db210e2d865cfe38b9f68b3e8; cross_origin_proto=SSL; _s_tentry=passport.weibo.com; Apache=2024617387008.776.1550912473467; SINAGLOBAL=2024617387008.776.1550912473467; ULV=1550912473477:1:1:1:2024617387008.776.1550912473467:; SSOLoginState=1550912517; SCF=AuK3Eq-JkG6jrMruEXJ9RR0OCRGsrMU-V0Ay0uXxQeNipn2-icVEfnyWzgroN6QNKAdKbeSH4t8hRVBs0N58LJU.; SUB=_2A25xdXxWDeRhGeNI7VYR8ibLzDmIHXVSA-qerDV8PUNbmtBeLWShkW9NSFL5eoIO_K5uDxqPlhiBWoGNMcTx70v-; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhYGb4-SAp5obSm.ebeXpMd5JpX5K2hUgL.Fo-cSoB7eonNS0-2dJLoIp7LxKML1KBLBKnLxKqL1hnLBoMfSoqXehzRS0Mf; SUHB=06Z1Udp4kRlhNk; ALF=1582448517; un=13836767328; wvr=6; ULOGIN_IMG=15509128764705; UOR=,,www.sogou.com; PHOTO-G0=8aae608225ce51dc489d4de036180247; WBStorage=201902231752|undefined; webim_unReadCount=%7B%22time%22%3A1550915600809%2C%22dm_pub_total%22%3A0%2C%22chat_group_pc%22%3A0%2C%22allcountNum%22%3A6%2C%22msgbox%22%3A0%7D',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'
        }
#数据为json格式
response = requests.get(url,headers = headers).json()
#print(response)
#new_url = pic_host+/sq612/+pic_name
pic_host = response['data']['photo_list'][0]['pic_host']  #按照所属关系得到pic_host
middle = '/sq612/'
pic_name = response['data']['photo_list'][0]['pic_name']
#print(pic_host)
#print(pic_name)

new_url = '%s%s%s'%(pic_host,middle,pic_name)  #将三者连接得到图片链接
#print(new_url)

r = requests.get(new_url)
#保存图片
with open('huge.jpg','wb') as f:
    f.write(r.content)   #写入图片,文本为text,图片为content

结果:
python爬虫--爬取微博图片_第9张图片

你可能感兴趣的:(爬虫)