【问题标题】:Scrapy extracts the data in any orderScrapy 以任意顺序提取数据
【发布时间】:2019-06-26 05:29:47
【问题描述】:

有人可以告诉我为什么 scrapy 会以任意顺序提取数据。我正在尝试在此页面上进行抓取。这个想法是从上到下提取所有数据房屋,然后转到下一页并执行相同的操作。 (直到第 20 页)

我不知道为什么我只拿一些房子,按任何顺序。谢谢

from scrapy.item import Field,Item 
from scrapy.spiders import CrawlSpider,Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.loader import ItemLoader
from scrapy.loader.processors import MapCompose


class PropietiItem(Item):
titulo=Field()
tipo= Field()

class PropietiCrawler(CrawlSpider):
name= "MiPrimerCrawler"
start_urls= ['https://www.propertyfinder.ae/en/search?c=2&l=1&ob=nd&page=1']
allowed_domains= ['propertyfinder.ae']

rules= (
    Rule(LinkExtractor(restrict_xpaths=('//div/a [@class="pagination__link pagination__link--next"]')),follow=True),
    Rule(LinkExtractor(restrict_xpaths=('//div [@class="card-list__item"]/a [@class="card card--clickable"]')),follow=True,callback= 'parse_items'),
)

def parse_items(self,response):
    item= ItemLoader(PropietiItem(),response)
    item.add_xpath('titulo','/html/body/main/div[1]/div/div[2]/div[2]/div[1]/div/h1/text()')
    item.add_xpath('tipo','/html/body/main/div[1]/div/div[2]/div[2]/div[1]/div/div/h2/text()')
    yield item.load_item()

【问题讨论】:

  • 请修正您的代码示例的缩进并删除重现您的问题不需要的任何代码(例如,MapCompose 已导入但未使用)。

标签: python database web-scraping request scrapy


【解决方案1】:

我了解到您正在尝试通过 html 代码搜索项目。

如果我不得不猜测,我会说它是按照它在 html 代码中找到的项目的顺序来做的。

可能某些项目具有您正在搜索的 html 代码,而其他项目的标签不同。

我建议首先检查丢失项目的 html 代码。如果这不起作用,我建议使用 Selenium。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2012-01-04
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多