马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 赤坂Dragon 于 2021-5-21 17:03 编辑 #-*- codeing = utf-8 -*-
import time
from bs4 import BeautifulSoup
import requests
import parsel
import re
import os
for page in range(1,12):
print(f'==========正在爬取第{page}页的图片==========')
url = f'https://www.tupianzj.com/bizhi/DNmeinv/list_77_{page}.html'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
response = requests.get(url=url,headers=headers)
html_data = response.text
#print(html_data)
a = []
c = []
selector = parsel.Selector(html_data)
href_list = selector.xpath('//div[@class="list_con_box"]/ul/li/a/@href').getall()
for i in href_list:
img_url = f'https://www.tupianzj.com{i}'
a.append(img_url)
#print(a)
for tite in range(0,int(len(a))):
response_tite = requests.get(url=a[tite],headers=headers)
html_data_tite = response_tite.text
selector_tite = parsel.Selector(html_data_tite)
href_list_tie = selector_tite.xpath('//div[@class="list_con_hot"]/ul/div/li/a/@href').getall()
b = ['https://www.tupianzj.com'+x for x in href_list_tie]
#print(b)
c.extend(b)
#print(c)
for p in c:
img_url_1 = 'p'
response_tite_q =requests.get(url=p,headers=headers)
html_data_tite_1 = response_tite_q.text
#print(html_data_tite_1)
selector_1 = parsel.Selector(html_data_tite_1)
href_list_y = selector_1.xpath('//div[@class="pictu900"]/center/div/div/a/img/@src').get()
#print(href_list_y)
img_url_v = requests.get(url=href_list_y,headers=headers).content
file_name = href_list_y.split('/')[-1]
with open('img\\' + file_name, mode='wb') as f:
f.write(img_url_v)
print('保存完成',file_name)
这是我目前写的源代码,目前只能爬取每页的第一张图片
https://www.tupianzj.com/bizhi/DNmeinv/这是目标网页
请问该如何实现里面图片的翻页处理,网上学的不适用于这种规则
这有关联吧,只有第一张没有 _数字 ,后续的和页码对应上的
参考代码:#-*- codeing = utf-8 -*-
import requests
import parsel
import re
url = 'https://www.tupianzj.com/meinv/20151025/30833.html'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36'}
response = requests.get(url=url,headers=headers)
response.encoding = "gb2312"
html_data = response.text
max_page = int(re.findall('共(\d+)页',html_data)[0])
base_url = 'https://www.tupianzj.com/meinv/20151025/30833_{}.html'
href_list = []
for page in range(1,max_page+1):
response = requests.get(url=url,headers=headers)
html_data = response.text
selector = parsel.Selector(html_data)
href = selector.xpath('//div[@id="bigpic"]/a/img/@src').getall()[0]
href_list.append(href)
url = base_url.format(page+1)
for p in href_list:
response_tite_q =requests.get(url=p,headers=headers)
img_url_v = response_tite_q.content
file_name = p.split('/')[-1]
with open(file_name, mode='wb') as f:
f.write(img_url_v)
print('保存完成',file_name)
|