python 圖片爬取 爬取各校校花圖片 —杜凱杰
爬取十頁?;▓D片(可按需求更改頁數,爬取更多圖片)—杜凱杰
import requests
from lxml import etree
startUrl='http://www.xiaohuar.com/list-1-'
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'
}
for i in range(10):
url=requests.get(startUrl+str(i)+'.html',headers=headers).content.decode('gbk')
story=etree.HTML(url)
photo_name=story.xpath('//div[@class="img"]')
try:
for i in photo_name:
name=i.xpath('./a/img/@alt')[0]
photo=i.xpath('./a/img/@src')[0]
photo='http://www.xiaohuar.com'+str(photo)
img = requests.get(photo,headers=headers,stream=True).content
with open('img/'+str(name)+'.jpg','wb') as f: //這里我用的是相對路徑保存的圖片,可自行修改為絕對路徑,避免下載后找不到文檔……
f.write(img)
print("正在下載:",name)
except:
print('當前頁面所有美眉圖片下載完畢!')
//如有不清楚的地方可發信息給我詢問http://www.dukaijie.com
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
