【问题标题】:Why is Scrapy not crawling/ parsing?为什么 Scrapy 不抓取/解析?
【发布时间】:2016-09-28 14:10:41
【问题描述】:

这可能是一个重复的问题。我正在尝试运行 Scrapy 蜘蛛,但无法运行。为什么我会收到错误消息“HtmlResponse 没有属性 urljoin”?如果 request_count 为 3 而 response_count 也为 3,那么 Scrapy 统计信息意味着什么?我的代码在这里。我将不胜感激在这件事上的任何帮助。

import scrapy
from scrapy.http.request import Request
from scrapy.spiders import BaseSpider
from scrapy.selector import HtmlXPathSelector

class BotSpider_2(BaseSpider):
    name = 'BotSpider_2'
    name = "google.co.th"
    start_urls = ["http://www.google.co.th/"]


    def parse(self, response):
        sel = Selector(response)
        sites = sel.xpath('//title/text()').extract()
        print sites

【问题讨论】:

    标签: scrapy


    【解决方案1】:

    首先,您的导入不正确。例如 - 为什么使用BaseSpider 而不是Spider?你也没有导入Selector。关于urljoin你描述的错误我没有看到你发布的代码抛出这个错误; urljoin 是自scrapy v1 左右以来的 Response 对象的一个​​函数,它将当前 url 与一些路径相结合,以创建可用于抓取的绝对 url。

    $ scrapy shell "https://scrapy.org"
    In [1]: response.url
    Out[1]: 'https://scrapy.org'
    
    In [2]: response.urljoin('/some/cool/path')
    Out[2]: 'https://scrapy.org/some/cool/path'
    

    我已经清理了导入,你的代码就像一个魅力!

    import scrapy
    from scrapy.selector import Selector
    
    class BotSpider_2(scrapy.Spider):
        name = "google.co.th"
        start_urls = ["http://www.google.co.th/"]
    
    
        def parse(self, response):
            sel = Selector(response)
            sites = sel.xpath('//title/text()').extract()
            print(sites)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      相关资源
      最近更新 更多