python爬虫学习05-爬取图片
-
确定要爬取的网址:https://shenan.tuchong.com/20903415/#image309854686
-
要爬取的内容:使用浏览器插件xpath对图片链接进行查找:
//article/img/@src
-
得到图片链接:
- 代码
import requests
from fake_useragent import UserAgent
from lxml import etree
url = "https://shenan.tuchong.com/20903415/#image309854686"
headers = {
"UserAgent":UserAgent().chrome
}
response = requests.get(url,headers=headers)
e = etree.HTML(response.text)
img_urls = e.xpath('//article/img/@src') #获取图片链接
print(img_urls)
for url in img_urls:
response = requests.get(url,headers=headers)
img_name = url[url.rfind('/')+1:] #命名
with open('img/'+img_name,'wb') as f: #写入到已存在的img文件夹中
f.write(response.content)