2017-11-13 5 views
0

私はscrapyフレームワークを使用していますが、分析されたWebページから他のデータを読み込むことはできません。あなたはそれについて何ができるか教えてください、ありがとう。もう1つのビュータグへのxpathで参照されているように

import scrapy 
from scrapy.spiders import CrawlSpider,Rule 
from scrapy.linkextractors import LinkExtractor 
from prueba1.items import Prueba1Item 
from scrapy.exceptions import CloseSpider 


class PruebaSpider(CrawlSpider): 
    name = 'prueba1' 
    item_count = 0 
    allowed_domain = ['http://www.abc.com.py/'] 
    start_urls = ['http://www.abc.com.py/buscar/?buscar=Santiago+Pe%C3%B1a' 
    'http://www.abc.com.py/buscar/?buscar=Santi+Pe%C3%B1a', 
    'http://www.abc.com.py/buscar/?buscar=santiago+pe%C3%B1a', 
    'http://www.abc.com.py/buscar/?buscar=santi+pe%C3%B1a'] 

    rules = { 


    Rule(LinkExtractor(allow =(),canonicalize = True, unique = 
    True,restrict_xpaths=('//html/body/div/a[@id="load-more"]'))), 
    Rule(LinkExtractor(allow =(),canonicalize = True, unique = 
    True,restrict_xpaths=('//div[@class="article"]')), 
    callback = 'parse_item', follow=True) 
    } 

    def parse_item(self, response): 
     ml_item=Prueba1Item() 

     ml_item['article'] = response.xpath('normalize-space(//h1)').extract() 
     ml_item['fecha'] = response.xpath('normalize- 
     space(//small)').extract() 
     ml_item['contenido'] = response.xpath('normalize- 
     space(//p[@class="summary"])').extract() 
     ml_item['contenido2'] = response.xpath('normalize- 
     space(//div[@class="text"])').extract() 
     ml_item['url'] = response.xpath('normalize- 
     space(//link/@href)').extract() 
     ml_item['comentarioFacebook'] = response.xpath('normalize- 
     space(//div[@class="_30o4"]/span/span[@class="_5mdd"])').extract() 

     self .item_count += 1 
     if self.item_count > 50: 
      raise CloseSpider('item_exceeded') 
     yield ml_item 

私は4000の以上の結果を持っていますが、私はこのコードで50以上を持って来ることができない、検索結果によります。

enter image description here

enter image description here

enter image description here

答えて

0

内容は、この形式でJSONを使用して動的にロードされます。

{ 
     "titulo": "Tuma se suma a Marito", 
     "copete": "El diputado \u00d3scar Tuma oficializ\u00f3 su respaldo a la candidatura de Mario Abdo Ben\u00edtez a la presidencia, ya que sus reportes indicaron que el candidato de Colorado A\u00f1etet\u00e9 tiene mayor intenci\u00f3n de votos. El 100% de su dirigencia se lo pidi\u00f3, dice. ", 
     "publicacion": "09-11-2017 08:00", 
     "imagen": "2017\/10\/02\/el-diputado-scar-tuma-inscribio-ayer-las-precandidaturas-de-su-movimiento-tu-asuncion-en-la-junta-de-gobierno--200750000000-1634915.jpg", 
     "url": "nacionales\/tuma-se-suma-a-marito-1648202.html", 
     "autor": "", 
     "hits": "3163", 
     "comentarios": "1", 
     "corte_url": "https:\/\/s3-sa-east-1.amazonaws.com\/assets.abc.com.py\/2017\/10\/02\/_146_162_1542245.jpg", 
     "corte_width": 146, 
     "corte_height": 162, 
     "autor_nombre": null, 
     "autor_url": "http:\/\/www.abc.com.py\/autor\/-.html", 
     "total": "4861" 
    } 

そこであなたはURLから直接JSON形式のデータを取得することができますXpathまたはcssセレクタを使用せずに以下のようにします()私はそれはあなたが望むデータを取得し、スクリプトにそれらを置くために、URLを変更することは困難ではないと信じ http://www.abc.com.py/ajax.php?seccion=busqueda-avanzada&tipo=4&tipoplant=0&buscar=Santiago+Pe%C3%B1a&desde=&hasta=&seccion-noticia=&temas=&begin=0&limit=7&aditional=

、例えば、2番目の項目から10項目を取得し、ちょうど変更:ブラウザでこのURL)エン10から2とlimitbegin:それはあなたの問題を解決した場合

http://www.abc.com.py/ajax.php?seccion=busqueda-avanzada&tipo=4&tipoplant=0&buscar=Santiago+Pe%C3%B1a&desde=&hasta=&seccion-noticia=&temas=&begin=2&limit=10&aditional=

+0

が貢献いただき、誠にありがとうございます、私はあなたが歓迎されている –

+0

それを証明しようと思って、答えを受け入れることを検討してください。 – luiyezheng

関連する問題