python - Python Crawler 对任何不同的 URL 请求返回相同的响应
问题描述
我正在构建一个非常简单的刮板,但是我在某个我无法找到的地方犯了一个非常愚蠢的错误。
在响应方法中,对于使用产品列表页面上所有产品的循环传递的任何 URL,我都会得到相同的响应
我在下面添加我的代码请帮忙。
def parse(self, response):
item = {}
count = 0
for single in response.xpath('//div[@class="_3O0U0u"]/div'):
count+=1
# print(count)
item['data_id'] = single.xpath('.//@data-id').extract_first()
item['price'] = single.xpath('.//div[@class="_1vC4OE"]/text()').extract_first()
item['url'] = single.xpath('.//div[@class="_1UoZlX"]/a[@class="_31qSD5"]/@href').extract_first()
if not item['url']:
item['url'] = single.xpath('.//div[@class="_3liAhj _1R0K0g"]/a[@class="Zhf2z-"]/@href').extract_first()
#print(item)
if item['url']:
yield scrapy.Request('https://www.somewebsite.com' + item['url'], callback = self.get_product_detail, priority = 1, meta={'item': item})
# break
next_page = response.xpath('//div[@class="_2zg3yZ"]/nav/a[@class="_3fVaIS"]/span[contains(text(),"Next")]/parent::a/@href').extract_first()
if next_page:
next_page = 'https://www.somewebsite.com'+response.xpath('//div[@class="_2zg3yZ"]/nav/a[@class="_3fVaIS"]/span[contains(text(),"Next")]/parent::a/@href').extract_first()
yield scrapy.Request(next_page, callback=self.parse ,priority=1)
def get_product_detail(self, response):
dict_item = response.meta['item']
sku = dict_item['data_id']
print('dict SKU ======== ', sku)
解决方案
推荐阅读
- javascript - 使用范围请求从服务器下载大文件
- html - 让我的专栏对所有手机屏幕都具有移动响应性?
- r - 如何为 ggplot 中的显着 p 值添加星号?
- angular - Heroku Angular 应用程序部署:加载资源失败:服务器响应状态为 404(未找到)
- javascript - React Stateful Class Component:使用“this.setState”更新状态的属性,不起作用。没有错误。状态不变
- python - 一个简单的操作需要永远的时间
- ubuntu - 修复停滞的 Xbuntu 安装的终端密码是什么?
- sql - 计算 SQL 数据库中视图中所有列的行数
- javascript - 在子组件中的componentDidMount中未定义来自异步父组件的道具数组
- docker - 如何在容器中下拉 docker 图像?