ma1998

python爬虫学习05-爬取图片

  1. 确定要爬取的网址:https://shenan.tuchong.com/20903415/#image309854686

  2. 要爬取的内容:使用浏览器插件xpath对图片链接进行查找://article/img/@src

  3. 得到图片链接:

  1. 代码
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)

分类:

技术点:

相关文章:

  • 2021-07-31
  • 2022-01-09
  • 2021-11-18
  • 2021-12-14
  • 2022-12-23
  • 2022-01-07
  • 2021-07-12
  • 2021-08-05
猜你喜欢
  • 2021-09-07
  • 2021-07-02
  • 2022-02-04
  • 2021-11-22
  • 2022-12-23
  • 2021-12-10
  • 2021-11-19
相关资源
相似解决方案