2017-09-04 24 views
0

こんにちは私はウェブサイトのニュースをクロールするためのスクラップを使用していますが、このプロセスを行うとエラーが発生します。ウェブサイトには多くのニュースページがあり、ニュースのURLはwww.example.com/34223ですこの問題を修正するための方法を見つけようと、彼女はscrapyのバージョンは1.4.0である私のコードであり、私はそれが今の仕事だMACOS詐欺師のウェブサイト

import scrapy 
from scrapy.spiders import CrawlSpider, Rule 
from scrapy.linkextractors import LinkExtractor 
class Example(scrapy.Spider): 
name = "example" 
allowed_domains = ["http://www.example.com"] 
start_urls = ["http://www.example.com"] 
    rules = (
    #self.log('testing rules' + response.url) 
     # Extract links matching 'category.php' (but not matching 'subsection.php') 
     # and follow links from them (since no callback means follow=True by default). 
     Rule(LinkExtractor(allow=('/*',), deny=(' ',))), 

     # Extract links matching 'item.php' and parse them with the spider's method parse_item 
     Rule(LinkExtractor(allow=('item\.php',)), callback='parse_item'), 
) 

def parse_item(self, response): 
    self.logger.info('Hi, this is an item page! %s', response.url) 
    item = scrapy.Item() 
     item['title'] = response.xpath('/html/body/div[3]/div/div/div[1]/div[1]/div/div[2]/text()').extract() 
     item['img_url'] = response.xpath('/html/body/div[3]/div/div/div[1]/div[1]/div/div[3]/img').extract() 
     item['description'] = response.xpath('/html/body/div[3]/div/div/div[1]/div[1]/div/div[5]/text()').extract() 
     return item 
+0

このエラーが発生すると、エラーが発生します。スパイダーエラー処理(リファラー:なし) – Raed

+0

'allowed_domains = [" http://www.example.com "]'を 'allowed_domains = [" www .example.com "]'、それが動作するかどうかを確認してください –

答えて

0

感謝を使用していますが、私はすべてのウェブサイトのニュースを投げる行く必要がありました

# -*- coding: utf-8 -*- 
import scrapy 

class ExampleSpider(scrapy.Spider): 
name = 'example' 
allowed_domains = ['www.Example.com'] 
start_urls = ['http://www.Example.com/1621305', 
] 

def parse(self, response): 
    for article in response.css('.article'): 
     yield { 
     'title' : article.css('.article-title h1::text').extract(), 
     'time' : article.css('.article-time time::text').extract(), 
     'article': article.css('.article-text p::text').extract(), 
     } 
0

コードを修正して正常に機能しています。はいはいこれを行いました。

# -*- coding: utf-8 -*- 
import scrapy 

class ExampleSpider(scrapy.Spider): 
name = 'example' 
allowed_domains = ['www. example.com'] 
start_urls = ['http://www.example.com/', 
] 

def parse(self, response): 
    for article in response.css('.main-news'): 
     yield { 
     'title'  : article.css('.article-title h1::text').extract(), 
     'time'  : article.css('.article-time time::text').extract(), 
     'another' : article.css('.article-source::text').extract(), 
     'section' : response.xpath('/html/body/div[3]/div/div/div[1]/ol/li[2]/a//text()').extract(), 
     'article' : article.css('.article-text p::text').extract() } 


    for next_page in response.css('a::attr(href)'): 
     yield response.follow(next_page, self.parse) 
関連する問題