【问题标题】:Scrapy crawling stackoverflow questions matching multiple tagsScrapy爬取匹配多个标签的stackoverflow问题
【发布时间】:2015-11-15 00:07:33
【问题描述】:

我现在正在尝试scrapy。我尝试了http://doc.scrapy.org/en/1.0/intro/overview.html 页面中的示例代码。我尝试使用标签“大数据”提取最近的问题。一切运作良好。但是当我尝试提取带有“bigdata”和“python”标签的问题时,结果不正确,结果中只有“bigdata”标签。但是在浏览器上,我对这两个标签都提出了正确的问题。请在下面找到代码:

import scrapy

class StackOverflowSpider(scrapy.Spider):
    name = 'stackoverflow'
    start_urls = ['https://stackoverflow.com/questions/tagged/bigdata?page=1&sort=newest&pagesize=50']

    def parse(self, response):
        for href in response.css('.question-summary h3 a::attr(href)'):
            full_url = response.urljoin(href.extract())
            yield scrapy.Request(full_url, callback=self.parse_question)

    def parse_question(self, response):
        yield {
            'title': response.css('h1 a::text').extract()[0],
            'votes': response.css('.question .vote-count-post::text').extract()[0],
            'body': response.css('.question .post-text').extract()[0],
            'tags': response.css('.question .post-tag::text').extract(),
            'link': response.url,
        }

当我将 start_urls 更改为

start_urls = ['https://stackoverflow.com/questions/tagged/bigdata+python?page=1&sort=newest&pagesize=50']

结果包含只有“大数据”标签的问题。如何只用两个标签来提问?

编辑:我认为正在发生的事情是,scrapy 正在从我提供的主页进入带有标签“bigdata”的页面,因为这些标签是指向该标签主页的链接。如何编辑此代码以使 scrapy 不进入标签页面而只进入该页面中的问题?我尝试使用如下规则,但结果仍然不正确。

rules = (Rule(LinkExtractor(restrict_css='.question-summary h3 a::attr(href)'), callback='parse_question'),)

【问题讨论】:

    标签: python web-scraping web-crawler scrapy


    【解决方案1】:

    您拥有的网址(以及最初的 css 规则)是正确的;或更简单地说:

    start_urls = ['https://stackoverflow.com/questions/tagged/python+bigdata']
    

    this 推断,这也可以:

    start_urls = ['https://stackoverflow.com/questions/tagged/bigdata%20python']
    

    但是,您遇到的问题是,stackoverflow 似乎要求您登录才能访问多标签搜索功能。要看到这一点,只需退出您的 stackoverflow 会话并在浏览器中尝试相同的 url。它只会将您重定向到两个标签中第一个的结果页面。

    TL;DR 获得多标签功能的唯一方法似乎是登录(通过会话 cookie 强制执行)

    因此,当使用scrapy 时,解决方法是在执行任何其他操作之前验证会话(登录),然后正常进行解析,一切正常。为此,您可以使用InitSpider 代替Spider 并添加适当的登录方法。假设您直接使用 StackOverflow 登录(而不是通过 Google 等),我能够让它按预期工作:

    import scrapy
    import getpass
    from scrapy.spiders.init import InitSpider
    
    class StackOverflowSpider(InitSpider):
        name = 'stackoverflow'
        login_page = 'https://stackoverflow.com/users/login'
        start_urls = ['https://stackoverflow.com/questions/tagged/bigdata+python']
    
        def parse(self, response):
            ...
    
        def parse_question(self, response):
            ...
    
        def init_request(self):
            return scrapy.Request(url=self.login_page, callback=self.login)
    
        def login(self, response):
            return scrapy.FormRequest.from_response(response,
                        formdata={'email': 'yourEmailHere@foobar.com',
                                  'password': getpass.getpass()},
                        callback=self.check_login_response)
    
        def check_login_response(self, response):
            if "/users/logout" in response.body:
                self.log("Successfully logged in")
                return self.initialized()
            else:
                self.log("Failed login")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 2014-08-22
      • 1970-01-01
      • 2016-02-23
      • 2023-03-05
      • 1970-01-01
      • 2023-01-27
      相关资源
      最近更新 更多