Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scrapy:ValueError(';请求url中缺少方案:%s';%self.\u url)_Python_Scrapy - Fatal编程技术网

Python Scrapy:ValueError(';请求url中缺少方案:%s';%self.\u url)

Python Scrapy:ValueError(';请求url中缺少方案:%s';%self.\u url),python,scrapy,Python,Scrapy,我正试图从网页上抓取数据。该网页只是一个包含2500个URL的项目符号列表。抓取并转到每个URL并获取一些数据 这是我的密码 class MySpider(CrawlSpider): name = 'dknews' start_urls = ['http://www.example.org/uat-area/scrapy/all-news-listing'] allowed_domains = ['example.org'] def parse(self, re

我正试图从网页上抓取数据。该网页只是一个包含2500个URL的项目符号列表。抓取并转到每个URL并获取一些数据

这是我的密码

class MySpider(CrawlSpider):
    name = 'dknews'
    start_urls = ['http://www.example.org/uat-area/scrapy/all-news-listing']
    allowed_domains = ['example.org']

    def parse(self, response):
        hxs = Selector(response)
        soup = BeautifulSoup(response.body, 'lxml')
        nf = NewsFields()
        ptype = soup.find_all(attrs={"name":"dkpagetype"})
        ptitle = soup.find_all(attrs={"name":"dkpagetitle"})
        pturl = soup.find_all(attrs={"name":"dkpageurl"})
        ptdate = soup.find_all(attrs={"name":"dkpagedate"})
        ptdesc = soup.find_all(attrs={"name":"dkpagedescription"})
         for node in soup.find_all("div", class_="module_content-panel-sidebar-content"):
           ptbody = ''.join(node.find_all(text=True))  
           ptbody = ' '.join(ptbody.split())
           nf['pagetype'] = ptype[0]['content'].encode('ascii', 'ignore')
           nf['pagetitle'] = ptitle[0]['content'].encode('ascii', 'ignore')
           nf['pageurl'] = pturl[0]['content'].encode('ascii', 'ignore')
           nf['pagedate'] = ptdate[0]['content'].encode('ascii', 'ignore')
           nf['pagedescription'] = ptdesc[0]['content'].encode('ascii', 'ignore')
           nf['bodytext'] = ptbody.encode('ascii', 'ignore')
         yield nf
            for url in hxs.xpath('//ul[@class="scrapy"]/li/a/@href').extract():
             yield Request(url, callback=self.parse)
现在的问题是,上面的代码在2500篇文章中大约占215篇。它通过给出此错误来结束

ValueError('请求url中缺少方案:%s'%self.\u url)

我不知道是什么导致了这个错误

非常感谢您的帮助


谢谢2019年1月更新

现在,Scrapy的响应实例有一个非常方便的方法
Response。follow
使用
Response.URL
作为基础,从给定URL(绝对或相对,甚至是
LinkExtractor
生成的
Link
对象)生成请求:

yield response.follow('some/url', callback=self.parse_some_url, headers=headers, ...)
文件:


下面的代码与问题类似:

 for url in hxs.xpath('//ul[@class="scrapy"]/li/a/@href').extract():
     yield Request(url, callback=self.parse)
如果任何URL不是完全限定的,例如看起来像
href=“/path/to/page”
而不是
href=”http://example.com/path/to/page“
您将看到错误。为确保生成正确的请求,您可以使用
urljoin

    yield Request(response.urljoin(url), callback=self.parse)

最简单的方法是使用
LinkedExtractor
尽管2019年1月更新

现在,Scrapy的响应实例有一个非常方便的方法
Response。follow
使用
Response.URL
作为基础,从给定URL(绝对或相对,甚至是
LinkExtractor
生成的
Link
对象)生成请求:

yield response.follow('some/url', callback=self.parse_some_url, headers=headers, ...)
文件:


下面的代码与问题类似:

 for url in hxs.xpath('//ul[@class="scrapy"]/li/a/@href').extract():
     yield Request(url, callback=self.parse)
如果任何URL不是完全限定的,例如看起来像
href=“/path/to/page”
而不是
href=”http://example.com/path/to/page“
您将看到错误。为确保生成正确的请求,您可以使用
urljoin

    yield Request(response.urljoin(url), callback=self.parse)
最糟糕的方法是使用
LinkExtractor