2017-02-03 40 views
0

ウェブページからデータをスクラップしようとしています。ウェブページは単に2500のURLの箇条書きのリストです。 Scrapyは今問題がある Scrapy:ValueError( 'リクエストURLのスキームがありません:%s'%self._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) 

です...フェッチし、それぞれ、すべてのURLに行くといくつかのデータをフェッチしている2500件の記事のうち215の周りに上記のコード命拾い。これは、( '要求URLに欠落スキーム:%s' は%self._url)...このエラーを与えることによって

とValueErrorを閉じ

私は、このエラーの原因を見当がつかない... 。

ご協力いただきありがとうございます。

おかげ以下

答えて

2

コードの問題のようになります。

for url in hxs.xpath('//ul[@class="scrapy"]/li/a/@href').extract(): 
    yield Request(url, callback=self.parse) 

のいずれかのURLは、例えば、完全修飾されていない場合href="http://domain.com/path/to/page"ではなくhref="/path/to/page"のように見えますが、エラーが発生します。あなたが正しい要求をもたらすことを確認するには、urljoinを使用することができます。

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

Scrapyの方法はLinkExtractor

https://doc.scrapy.org/en/latest/topics/link-extractors.htmlかかわらを使用することです
関連する問題