python用xpath爬取10页网站图片
生活随笔
收集整理的這篇文章主要介紹了
python用xpath爬取10页网站图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#爬取網站圖片
import requests
from lxml import etree
import osi=0 #計數
#請求頭
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"}
for x in range(1,11):html="https://www.zerochan.net/Yukinoshita+Yukino?p="+str(x) #一共10頁圖片r=requests.get(html,headers=headers) #請求響應#解析網頁內容html=etree.HTML(r.text)root="F://z yukinoshita/" #需要存儲的根目錄if not os.path.exists(root): #判斷根目錄是否存在os.mkdir(root) #不存在則創建文件夾url=html.xpath('//*[@id="thumbs2"]/li/a/img/@src') #從網頁信息中獲取圖片鏈接z=len(url)for y in range(0,z):html_1="https://static.zerochan.net/Yukinoshita.Yukino.full."+url[y].split('/')[-1]image_name=html_1.split('/')[-1] #圖片的名字path=root+image_name #圖片在電腦的路徑及名字try:if not os.path.exists(path): #如果電腦中不存在該圖片with open(path,'wb') as f: #把圖片寫進圖片文件con=requests.get(html_1) #請求圖片鏈接的信息f.write(con.content) #將圖片的二進制寫入文件i=i+1print("爬取成功"+str(i))else:i=i+1print("文件已存在"+str(i)) except: i=i+1print("爬取失敗"+str(i))
總結
以上是生活随笔為你收集整理的python用xpath爬取10页网站图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个日一个翟读什么 一个日一个翟是什么字
- 下一篇: python中if __name__ =