首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >刮擦爬行器返回重复值

刮擦爬行器返回重复值
EN

Stack Overflow用户
提问于 2020-06-06 14:50:20
回答 1查看 158关注 0票数 0

下面是我的完整代码。我不知道它为什么还了很多副本。有什么解决办法吗?我试图要求所有的区域从这个链接"https://www.compass.com/agents“,并提取代理的信息。

代码语言:javascript
复制
# -*- coding: utf-8 -*-
import scrapy


class MainSpider(scrapy.Spider):
    name = 'main'
    start_urls = ["https://www.compass.com/agents"]

    def parse(self, response):
        regions = response.xpath('//ul[@class="geoLinks-list textIntent-caption1--strong"]/li')
        for each in regions:
            region_link = each.xpath('.//a/@href').get()
            region_name = each.xpath('.//a/text()').get()

            yield response.follow(url=region_link, callback=self.parse_data, meta={"region_text": region_name})


    def parse_data(self, response):
        region = response.request.meta["region_text"]

        agents = response.xpath('//div[@class="agentCard-contact"]')
        for agent in agents:
            name = agent.xpath('normalize-space(//div[@class="agentCard-contact"]/a/text())').get()
            profile_link = agent.xpath('//div[@class="agentCard-contact"]/a/@href').get()
            email = agent.xpath('//a[@class="textIntent-body agentCard-email"]/@href').get()
            mobile = agent.xpath('//a[@class="textIntent-body agentCard-phone"]/@href').get()

            yield {
                "Name": name,
                "Profile_link": profile_link,
                "Email": email,
                "Mobile": mobile,
                "Region": region,
            }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-06 15:50:03

我想这是xpath的问题。用下面的内容更改xpath并尝试:name = agent.xpath('normalize-space(.//a[@class="textIntent-headline1 agentCard-name"]/text())').get() profile_link = agent.xpath('.//a[@class="textIntent-headline1 agentCard-name"]/@href').get() email = agent.xpath('.//a[@class="textIntent-body agentCard-email"]/@href').get() mobile = agent.xpath('.//a[@class="textIntent-body agentCard-phone"]/@href').get()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62233396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档