为了账号安全,请及时绑定邮箱和手机立即绑定

Scrapy:如何从页面上的所有选项卡获取信息?

Scrapy:如何从页面上的所有选项卡获取信息?

慕尼黑5688855 2023-08-29 18:08:52
在此页面上,我需要从所有选项卡(个人资料、评论、电话号码和方向)获取信息。wellness.pydef profile(self, response):    services = response.xpath('.//span[contains(text(),"Services")]')    education = response.xpath('.//span[contains(text(),"Education")]')    training = response.xpath('.//span[contains(text(),"Training")]')    yield {            'First and Last name': response.css('h1::text').get(),            'About': response.css('.listing-about::text').get(),            'Services': services.xpath('following-sibling::span[1]/text()').extract(),            'Primary Specialty': response.css('.normal::text').get(),            'Address': ' '.join([i.strip() for i in response.css('.office-address span::text').getall()]),            'Practice': response.css('.years-in-service::text').get(),            'Education': education.xpath('following-sibling::span[1]/text()').extract(),            'Training': training.xpath('following-sibling::span[1]/text()').extract(),            'Consumer Feedback': response.css('.item-rating-container a::text').get()                        }
查看完整描述

1 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

每个选项卡都加载一个单独的页面/url。我想你认为既然它被标记为同一页面。因此,您必须从第一页收集所需的数据,请求第二页获取数据,然后请求第三页。您可以通过在元属性中传递项目来保留上一页的数据。我就是这样做的。请注意,链接的代码是正确的,您必须为每个页面上的数据点创建选择器。


def profile(self, response):

    item = {}

    item["field1"] = response.xpath('//xpath').get()

    # Get first link for reviews

    review_link = response.css('#reviews_tab a::attr(href)').get()

    yield scrapy.Request(response.urljoin(review_link), callback=self.parse_reviews, meta={'item': item})


def parse_reviews(self, response):

    item = response.meta['item']

    item["field2"] = response.xpath

    directions_link = response.css('#directions_tab a:attr(href)').get()

    yield scrapy.Request(response.urljoin(directions_link), callback=self.parse_directions, meta={'item': item})


def parse_directions(self, response):

    item = response.meta['item']

    item['directions'] = response.xpath

    yield item


查看完整回答
反对 回复 2023-08-29
  • 1 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信