# -*- coding: utf-8 -*-
"""
Created on Sat Apr 14 10:28:35 2018
@author: Administrator
"""
global pic_link
#pic_link=[]
import requests
#import random
from lxml import etree
def load_image(url):
headers={"User-Agent":"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"}
html=requests.get(url,headers=headers)
content=etree.HTML(html.text)
link=content.xpath('//*[@id="bigpicpic"]/@src',stream=True)
return str(link[0])
def save_image(pic_link,i):
with open('D:/python/python_work/CSDN/pic_{}.gif'.format(i),'wb') as f:
picture=requests.get(pic_link).content
f.write(picture)
if __name__=='__main__':
for i in range (1, 21):
url='http://photo.hupu.com/p10087-{}.html'.format(i)
tail=load_image(url)
pic_link='http:'+tail
save_image(pic_link,i)
最近在学XPATH,随便模仿写了段爬虎扑搞笑图片的代码。
网友评论