【问题标题】:My Scrapy spider can't extract data from the next page我的 Scrapy 蜘蛛无法从下一页提取数据
【发布时间】:2023-03-11 07:43:02
【问题描述】:

所以我被要求从网站上抓取所有工作详细信息,但是我的蜘蛛成功获取到下一页的链接,但只从第一个页面中提取数据
这是我的蜘蛛:

name = 'jobs'
allowed_domains = ['www.tanitjobs.com/jobs']
start_urls = ['https://www.tanitjobs.com/jobs']

def parse(self, response):
    pass

    all_jobs = response.css(".listing-item__jobs")

    for job in all_jobs:
        item = {
            'jobname' : job.css("article.listing-item div.listing-item__title a::text").getall(),
            "companyname" : job.css(".listing-item__info--item-company::text").extract(),
            "city" : job.css(".listing-item__info--item-location::text").extract() ,
            }

        yield item

    next_page = response.css(".pad_right_small a ::attr(href)").extract_first()
    if next_page:
       next_page = response.urljoin(next_page)
       yield scrapy.Request(url=next_page, callback=self.parse)

This is the result that i got after running the spider

如果有人知道问题出在哪里,我真的需要你的帮助并提前感谢。

【问题讨论】:

  • 我认为 a 和 ::attr(href) 之间的空格是错误的,您可能需要将链接设为绝对链接。
  • @pguardiario 这是绝对的,我只是使用 urljoin 来实现这一点

标签: python web-scraping pagination scrapy


【解决方案1】:

allowed_domains = ['www.tanitjobs.com/jobs']

因为它的变量名是一个死的赠品,所以应该只将允许的 domains 放在该列表中,而您所拥有的是一个 partial URL,这会导致拒绝请求的异地过滤器

除非您有特殊需要,否则我建议仅在该值中列出基本

allowed_domains = ['tanitjobs.com']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多