因为他爬回来有的页数据是方块 但重新爬取一下就不是了 我想让他重新爬取这个方块数据的页 但是为什么except一直在爬这个页出不来了啊 应该怎么写 怎么办啊
import requests
import parsel
for page in range(1,168):
def get_html():
print("函数开始")
cars_data=parsel.Selector(response.text)
lis=cars_data.css('#__next > div.tw-flex > div.new-main.new > div > div > div.jsx-2898915557.wrap > ul > li')
# return lis
# def get_inf(lis_1):
for li in lis:
car_title=li.css(' li > a > dl > dt > p::text').get()
car_info_1=li.css('li > a > dl >dd:nth-child(2)::text').getall()
car_info_2=li.css('li > a > dl >dd:nth-child(3)>span::text').getall()
car_price_1 = li.css('li > a > dl >dd:nth-child(4)::text').get().replace('万','')
car_price_2=li.css('li > a > dl >dd:nth-child(4)>span::text').get().replace('新车指导价:','').replace('万','')
car_years=''.join(car_info_1).split('|')[0]
car_gonli=''.join(car_info_1).split('|')[1].replace('万公里','')
car_posotion = ''.join(car_info_1).split('|')[2]
if len(car_info_2)==2:
car_jianche=car_info_2[0]
car_guohu=car_info_2[0]
else:
car_jianche="无检测报告"
car_guohu=car_info_2[0]
for i in car_price_1:
try:
int(i)
print(car_title, car_years, car_gonli, car_posotion, car_jianche, car_guohu, car_price_1,
car_price_2)
break
except :
print(i)
print("错误开始",page)
print(car_title, car_years, car_gonli, car_posotion, car_jianche, car_guohu, car_price_1,
car_price_2)
get_html()
# get_inf(lis_5)
print("错误结束")
break
continue
print("跳出循环")
print("函数结束")
get_html()
print('---------------------------------------第', page, '页---------------------------------------')
原始数据:
pairs = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('one', 11)]
转化成一键多值的字典形式:
{'one': [1,11], 'two': [2], 'three': [3], 'four': [4]}
方法一:
pairs = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('one', 11)]
d = {}
for key, value in pairs:
if key not in d:
d[key] = []
d[key].append(value)
print(d)
# Out:{'one': [1, 11], 'two': [2], 'three': [3], 'four': [4]}
方法二:
from collections import defaultdict
pairs = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('one', 11)]
d = defaultdict(list)
for key, value in pairs:
d[key].append(value) # 省去了if判断语句
print(d)
# Out:defaultdict(<class 'list'>, {'one': [1, 11], 'two': [2], 'three': [3], 'four': [4]})
针对问题,出现乱码或方块可能是由于页面编码格式不对或者数据加密等原因导致的,可以尝试解决以下几个方面:
import requests
from bs4 import BeautifulSoup
url = 'https://example.com'
response = requests.get(url)
response.encoding = 'utf-8' # 设置编码格式
soup = BeautifulSoup(response.text, 'html.parser')
检查数据是否加密:可以观察页面中网络请求的数据,在浏览器的开发者工具中查看,在跟踪请求时是否出现加密的操作。如果出现加密,则需要解密该数据,才能进行正确的解析和显示。
尝试使用自动化测试工具重新爬取数据:可以使用Selenium等自动化测试工具,模拟浏览器操作,重新爬取数据。示例代码如下:
from selenium import webdriver
url = 'https://example.com'
browser = webdriver.Chrome() # 需要下载对应的ChromeDriver,放在环境变量中或指定路径
browser.get(url)
soup = BeautifulSoup(browser.page_source, 'html.parser')
browser.quit()
for page in pages:
try:
# 解析该页面的数据
# ...
except Exception as e:
print(e) # 输出错误信息
# 重新爬取该页面
response = requests.get(page)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
需要注意的是,多次重新爬取同一页面可能会被网站的反爬虫机制封禁IP,需要适当控制重新爬取的次数和频率。同时,在爬取数据时需要遵守网站的robots.txt协议,不要过度频繁地爬取对方网站的数据。
可能是一直出错,导致出现了死循环