【问题标题】:scraping web page containing anchor tag <a href = "#"> using scrapy使用 scrapy 抓取包含锚标签 <a href = "#"> 的网页
【发布时间】:2018-04-09 20:07:42
【问题描述】:

我在刮manulife

我想转到下一页,当我检查“下一个”时,我得到:

<span class="pagerlink">
    <a href="#" id="next" title="Go to the next page">Next</a>
</span>

什么是正确的做法?

# -*- coding: utf-8 -*-
import scrapy
import json
from scrapy_splash import SplashRequest

class Manulife(scrapy.Spider):
    name = 'manulife'
    #allowed_domains = ['https://manulife.taleo.net/careersection/external_global/jobsearch.ftl?lang=en']
    start_urls = ['https://manulife.taleo.net/careersection/external_global/jobsearch.ftl?lang=en&location=1038']

    def start_requests(self):
        for url in self.start_urls:
            yield SplashRequest(
            url,
            self.parse,
            args={'wait': 5},
            )   

    def parse(self, response):
        #yield {
        #   'demo' : response.css('div.absolute > span > a::text').extract()
        #     }
        urls = response.css('div.absolute > span > a::attr(href)').extract() 
        for url in urls:
            url = "https://manulife.taleo.net" + url
            yield SplashRequest(url = url, callback = self.parse_details, args={'wait': 5})
            #self.log("reaced22 : "+ url)

        #hitting next button
        #data = json.loads(response.text)
        #self.log("reached 22 : "+ data)
        #next_page_url = 

        if next_page_url:
           next_page_url = response.urljoin(next_page_url) 
           yield SplashRequest(url = next_page_url, callback = self.parse, args={'wait': 5})

    def parse_details(self,response):
        yield {
           'Job post' : response.css('div.contentlinepanel > span.titlepage::text').extract(),
           'Location' : response.xpath("//span[@id = 'requisitionDescriptionInterface.ID1679.row1']/text()").extract(),
           'Organization' : response.xpath("//span[@id = 'requisitionDescriptionInterface.ID1787.row1']/text()").extract(),
           'Date posted' : response.xpath("//span[@id = 'requisitionDescriptionInterface.reqPostingDate.row1']/text()").extract(),
           'Industry': response.xpath("//span[@id = 'requisitionDescriptionInterface.ID1951.row1']/text()").extract()
          }

如您所见,代码在点击下一页链接时包含 SplashRequest。

我是抓取新手,在某个地方我发现该网站也可以将响应返回为 json。我试过了,但它给了我“无法解码任何 json 对象”的错误

【问题讨论】:

  • 我也尝试过使用scrapy-splash,但没有结果。
  • scrapy 无法解释 javascript,使用 selenium 来处理这些事情。
  • 我使用了用于处理javascript请求的scrapy-splash。 @shotgunner
  • 我用过scrapy-splash...显示你的代码
  • 添加了代码@Andersson 我是新手,在某个地方我发现网站也可以将响应返回为 json。我试过了,但它给了我“无法解码任何 json 对象”的错误

标签: javascript python web-scraping scrapy scrapy-splash


【解决方案1】:

我认为像这样使用 css 选择器 ".pagerlink a[title='Go to the next page']" 可以工作。

".pagerlink:last-child a" 将是 imo 的最佳方法。您只需要获取 href 属性

【讨论】:

  • 这只是给出了包含“#”的锚标记。所以它没有用。 ://
猜你喜欢
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多