马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import requests,json
from lxml import etree
def gethtml(url):
headers={
"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
"Cookie":"",
"Referer":"https://pic.sogou.com/pics?query=%E9%A2%86%E5%85%8B",
}
r=requests.get(url,headers=headers)
if r.status_code !=200:
print("获取url:%s失败!"%(url))
exit()
return r
def getxpath(r,str):
html = etree.HTML(r.text)
reslist = html.xpath(str)
return reslist
def writetxt(txtpath,data_str):
with open(txtpath, "w") as f:
f.write(str(data_str))
def writeimg(imgpath,content):
with open(imgpath, "wb") as f:
f.write(content)
def get_sogou_jpg():
url='https://pic.sogou.com/pics?query=%E9%A2%86%E5%85%8B'
r=gethtml(url)
jpg_url_list=getxpath(r,'//img[@id="videoApp"]/div/div[2]/div/ul/li[1]/div/a[1]/img/@src')
jpg_url=jpg_url_list[0]
jpg_url="https:"+jpg_url
r=gethtml(jpg_url)
writeimg('./搜狗jpg.png',r.content)
print('恭喜爬到图片')
if __name__ == '__main__':
get_sogou_jpg()
print('恭喜')
求解为啥超出范围, |