python爬虫
import urllibimport urllib.request
import time
import re
for p in range(1,8):
url='https://www.tupianzj.com/meinv/20200324/207357_'+str(p)+'.html'
headers={'UA':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
data=requests.get(url,headers=headers)
data.encoding='utf-8'
html=etree.HTML(data.text)
license=html.xpath("//ul[@class='list_con_box_ul']/li/a/@href")
print(license)
print('第'+str(p)+'页所有图片网址获取完毕!')
print('____________________________________')
print('正在下载'+str(p)+'页所有图片,')
time.sleep(8)
for i in range(0,len(license)):
print('正在爬取'+str(p)+'页的第'+str(i+1)+'位')
url_pic="https://www.tupianzj.com"+str(license)
headers={'UA':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
data=requests.get(url_pic,headers=headers)
data.encoding='utf-8'
html=etree.HTML(data.text)
page=html.xpath("//div[@class='pages']/ul/li/a/text()")
page=re.findall("\照片",page)
url_pics=url_pic.split('.',-1)
url_pics=url_pics+'.'+url_pics+'.'+url_pics
for j in range(1,int(page)):
url_page=url_pics+'_'+str(j+1)+'.html'
headers={'UA':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
data=requests.get(url_page,headers=headers)
data.encoding='utf-8'
html=etree.HTML(data.text)
pages=html.xpath("//div[@id='bigpic']/a/img/@src")
print(pages)
time.sleep(1)
urllib.request.urlretrieve(pages,'D:/新建文件夹/'+str(p)+'.'+str(i+1)+'.'+str(j)+'.jpg')
print('下载结束了')
(新手一枚)请大家看下 ,最后我保存到哪里去了? 我文件夹里也没有
爬虫的基础是url不能失效
你爬的页面都404了,肯定什么都爬不出来 url="https://www.tupianzj.com/meinv/20210609/228790"
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
import requests
from lxml import etree
pic=[]
for x in range(1,9):
if x==1:
_url=".html"
else:
_url="_"+str(x)+".html"
res=requests.get(url+_url,headers=headers)
obj=etree.HTML(res.text)
pic.append(obj.xpath("//img[@id='bigpicimg']/@src"))
for x in pic :print(x)
页:
[1]