1. 程式人生 > >python爬蟲(Xpath)

python爬蟲(Xpath)

import requests 
from lxml import etree 
url = 'http://tieba.baidu.com/p/2166231880' 
header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'} 
r = requests.get(url,headers=header).content 
s = etree.HTML(r)
a=s.xpath('//div/img/@src')
b=0
for i in a:
try:
with open('C:\\Users\Administrator\\Desktop\\Python\\實寫爬蟲\\圖片\\'+i[-9:-4]+'.jpg','wb') as f:
print(i)
text=(requests.get(i,headers=header).content)
f.write(text)
b=b+1
except:
print('完畢')
break