错误响应“NoneType”对象在使用scrapy抓取网站时不可编辑

2024-09-29 21:57:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我对使用scrapy进行网页抓取是新手。我试图刮一个网站(请参考代码中的网址)。 从网站上,我试图删除'notification For%Month%%Year%'表下的信息,并将数据传输到json文件中。在

I am getting an error as "'NoneType' object is not iterable",while executing the command:

scrapy crawl quotes -o quotes.json

代码:

import scrapy
class QuotesSpider(scrapy.Spider):
  name = "quotes"

def start_requests(self):
    urls = [
       'http://www.narakkalkuries.com/intimation.html#i'
    ]

def parse(self, response):
  for check in response.xpath('//table[@class="MsoTableGrid"]'):
    yield{
           'data':check.xpath('//table[@class="MsoTableGrid"]/tr/td/p/b//text()').extract_first()
         }

问题: 在网站中,所有的提示数据都存储在同名的表下表@类=“MsoTableGrid”。在

选项是我试图提取数据

Option1

response.xpath('//table[@class="MsoTableGrid"]').extract()

Return all the data

选项2

response.xpath('//table[@class="MsoTableGrid"]/tr[i]/td/p/b').extract()

Return few of the vertical column

选项3

response.xpath('//table[@class="MsoTableGrid"]/tr/td/p/b//text()').extract()[1]

Return first element from the whole data

问题:

  • 使用Option3时,是否可以知道返回的元素是否为字符串?在
  • 在使用Option3时,是否可以知道返回的整个数据范围,以便遍历每个返回的元素?在
  • 如何修复“NoneType”object is not iterable错误

Tags: the数据data网站response选项tableextract
2条回答

要添加到这一点,start_requests应该是scrapy.Request对象的生成器。您的start_requests没有产生任何结果:

def start_requests(self):
    urls = [
       'http://www.narakkalkuries.com/intimation.html#i'
    ]

要修复在start_requests方法中逐个生成URL的问题:

^{pr2}$

或者只需设置start_urlsclass属性,就可以使用从scrapy.Spider继承的默认start_requests方法:

import scrapy
class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = [
        'http://www.narakkalkuries.com/intimation.html#i'
    ]
import scrapy


class QuotesSpider(scrapy.Spider):
  name = "quotes"

def start_requests(self):
    urls = [
       'http://www.narakkalkuries.com/intimation.html#i'
    ]

    # Here you need to yield the scrapy.Request
    for url in urls:
        yield scrapy.Request(url)

def parse(self, response):
  for check in response.xpath('//table[@class="MsoTableGrid"]'):
    yield{
           'data':check.xpath('//table[@class="MsoTableGrid"]/tr/td/p/b//text()').extract_first()
         }

相关问题 更多 >

    热门问题