【问题标题】:scrapy crawler showing error while crawlingscrapy 爬虫在爬取时显示错误
【发布时间】:2017-07-14 11:54:55
【问题描述】:

我正在尝试抓取优惠券网站的优惠券,但是当我在 尝试运行爬虫显示错误。请帮助。 谢谢。

import scrapy
from scrapy.http import Request
from scrapy.selector import HtmlXPathSelector
from scrapy.spider import BaseSpider
class CuponationSpider(scrapy.spider):
   name = "cupo"
   allowed_domains = ["cuponation.in"]
   start_urls = ["https://www.cuponation.in/firstcry-coupon#voucher"]
   def parse(self, response):
      all_items = []
      divs_action = response.xpath('//div[@class="action"]')
      for div_action in divs_action:
         item = VoucherItem()
         span0 = div_action.xpath('./span[@data-voucher-id]')[0]
         item['voucher_id'] = span0.xpath('./@data-voucher-
                  id').extract()[0]
         item['code'] = span0.xpath('./span[@class="code-
               field"]/text()').extract()[0]
         all_items.append(item)





   >**Output** ERROR  
File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err)URLError: <urlopen error timed out>
2017-07-25 16:36:59 [boto] ERROR: Unable to read instance data, giving 
 up

【问题讨论】:

  • 您的问题的答案在警告中。不要使用 scrapy.selector.HtmlXPathSelector 使用 scrapy.Selector
  • @Neil 问题仍未解决,我也试过了。
  • 那么现在的警告是什么?错误是什么?
  • @Neil---File "/home/abhinav/Coupons/Voucher/Voucher/spiders/Couponation.py",第 13 行,解析中 hxs = scrapy.Selector(response) NameError: global name 'scrapy' 没有定义
  • 上面的代码是你完整的scrapy文件吗?请检查你的缩进

标签: python xml xpath scrapy web-crawler


【解决方案1】:

评论:...告诉我我在做什么的错误

  1. 删除所有import 行,只使用一个:

    import scrapy
    
  2. 你的类继承应该是:

    class CuponationSpider(scrapy.Spider):
    
  3. 你已经改变了namestarturl,使用:

    name = "cuponation"
    allowed_domains = ['cuponation.in']
    start_urls = ['https://www.cuponation.in/firstcry-coupon']
    
  4. 您使用 Python 2.7
    抱歉,无法使用 2.7 运行 Scrapy。这可能是不同之处。
    ERROR: Unable to read instance data, give 表示您没有从给定的 URL 收到任何数据。也许您已被列入黑名单。

评论:网址是 cuponation.in/firstcry-coupon#voucher

这是相同页面,无需重新加载。
一切都可以简化为:

all_items = []

def parse(self, response):
    # Get all DIV with class="action"
    divs_action = response.xpath('//div[@class="action"]')

    for div_action in divs_action:
        item = VoucherItem()

        # Get SPAN from DIV with Attribute data-voucher-id
        span0 = div_action.xpath('./span[@data-voucher-id]')[0]

        # Copy Attribute voucher_id
        item['voucher_id'] = span0.xpath('./@data-voucher-id').extract()[0]

        # Find SPAN class="code-field" inside span0 and copy Text
        item['code'] = span0.xpath('./span[@class="code-field"]/text()').extract()[0]

        all_items.append(item)

输出

#CouponSpider.start_requests:https://www.cuponation.in/firstcry-coupon
#CouponSpider.parse()
#CouponSpider.divs_action:List[13] of <Element div at 0xf6b1c20c>
{'voucher_id': '868600', 'code': '*******'}
{'voucher_id': '31793', 'code': '*******'}
{'voucher_id': '832408', 'code': '*******'}
{'voucher_id': '819903', 'code': '*******'}
{'voucher_id': '808774', 'code': '*******'}
{'voucher_id': '32274', 'code': '*******'}
{'voucher_id': '32102', 'code': '*******'}
{'voucher_id': '844247', 'code': '*******'}
{'voucher_id': '843513', 'code': '*******'}
{'voucher_id': '848151', 'code': '*******'}
{'voucher_id': '845248', 'code': '*******'}
{'voucher_id': '869101', 'code': '*******'}
{'voucher_id': '869328', 'code': '*******'}            

【讨论】:

  • @stovfl-----我上传了所有代码,但仍然遇到问题。
  • 我已完成所有更改,但仍然无法获得结果。
  • @stovfi -- 我只更改了 xpath,并且该 url 正在使用我想要获取的正确优惠券 ID..
  • @abhi09sep:我的答案代码有效!使用 Python 3.4 测试
  • @stovfl-- 我已经按照你的要求更新了代码,但仍然没有得到输出..
猜你喜欢
  • 2019-06-24
  • 2017-11-02
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-14
相关资源
最近更新 更多