分析網頁數據結構html
接下來找張圖片右擊點擊檢查python
import requests from lxml import etree # 設計模式 --》面向對象編程 class Spider(object): def __init__(self): # 反反爬蟲措施,加請求頭部信息 self.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36", "Referer": "https://www.mzitu.com/xinggan/" } def start_request(self): # 1. 獲取總體網頁的數據 requests for i in range(1, 204): print("==========正在抓取%s頁==========" % i) response = requests.get("https://www.mzitu.com/page/"+ str(i) + "/", headers=self.headers) html = etree.HTML(response.content.decode()) self.xpath_data(html) def xpath_data(self, html): # 2. 抽取想要的數據 標題 圖片 xpath src_list = html.xpath('//ul[@id="pins"]/li/a/img/@data-original') alt_list = html.xpath('//ul[@id="pins"]/li/a/img/@alt') for src, alt in zip(src_list, alt_list): file_name = alt + ".jpg" response = requests.get(src, headers=self.headers) print("正在抓取圖片:" + file_name) # 3. 存儲數據 jpg with open try: with open(file_name, "wb") as f: f.write(response.content) except: print("==========文件名有誤!==========") spider = Spider() spider.start_request()
哎!好像沒問題!編程
本公衆號只出精品,拒收劣質設計模式