用requests爬取图片

╰半夏微凉° 2021-12-16 14:59 402阅读 0赞
  1. # coding=utf-8
  2. from bs4 import BeautifulSoup
  3. import requests
  4. import urllib
  5. x = 1
  6. def crawl(url):
  7. res = requests.get(url)
  8. soup = BeautifulSoup(res.text, 'html.parser')
  9. yinhuns = soup.select('img[id="bigImg"]')
  10. for yh in yinhuns:
  11. link = yh.get('src')
  12. print(link)
  13. global x
  14. # 写地址的时候,由于用了"\",导致"unicodeescape’ codec can’t decode bytes in position XXX: trun"错误
  15. # 建议改成"/"
  16. urllib.request.urlretrieve(link, f'C:/Users/Administrator/Desktop/alien/pachong/yinhun/{x}.jpg')
  17. print(f'正在下载第{x}张图片')
  18. x += 1
  19. for i in range(15,24):
  20. url = "http://desk.zol.com.cn/bizhi/7482_927" + str(i) + "_2.html"
  21. try:
  22. crawl(url)
  23. except ValueError as f:
  24. continue
  25. except Exception as e:
  26. print(e)

转载于:https://www.cnblogs.com/rener0424/p/10969439.html

发表评论

表情:
评论列表 (有 0 条评论,402人围观)

还没有评论,来说两句吧...

相关阅读