从多个URL刮取数据

时间:2014-06-11 18:04:52

标签: python web-scraping scrapy scrapy-spider

我希望从[链接] http://cbfcindia.gov.in/html/SearchDetails.aspx?mid=1&Loc=Backlog中抓取数据!  ,但是MID参数在URL中是递增的,以提供第2个,第3个URL .....直到1000个URL,所以我该如何处理这个(我是PYTHON和SCRAPY的新手,所以不介意我问这个)?

请检查我用来提取信息的XPATH,它是没有输出,蜘蛛是否存在基本错误

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from movie.items import MovieItem

class MySpider(BaseSpider):
    name = 'movie'
    allowed_domains= ["http://cbfcindia.gov.in/"]
    start_urls = ["http://cbfcindia.gov.in/html/SearchDetails.aspx?mid=1&Loc=Backlog"]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        titles = hxs.select("//body")    #Check
        print titles
        items = []
        for titles in titles:
          print "in FOR loop"
          item = MovieItem()
                  item ["movie_name"]=hxs.xpath('//TABLE[@id="Table2"]/TR[2]/TD[2]/text()').extract()
          print "XXXXXXXXXXXXXXXXXXXXXXXXX  movie name:", item["movie_name"]
          item ["movie_language"] = hxs.xpath('//*[@id="lblLanguage"]/text()').extract()
          item ["movie_category"] = hxs.xpath('//*[@id="lblRegion"]/text()').extract()
          item ["regional_office"] = hxs.xpath('//*[@id="lblCertNo"]/text()').extract()
          item ["certificate_no"] = hxs.xpath('//*[@id="Label1"]/text()').extract()
          item ["certificate_date"] = hxs.xpath('//*@id="lblCertificateLength"]/text()').extract()
          item ["length"] = hxs.xpath('//*[@id="lblProducer"]/text()').extract()
          item ["producer_name"] = hxs.xpath('//*[@id="lblProducer"]/text()').extract()

          items.append(item)

          print "this is ITEMS"
        return items

以下是日志:

log>
    {'certificate_date': [],
     'certificate_no': [],
     'length': [],
     'movie_category': [],
     'movie_language': [],
     'movie_name': [],
     'producer_name': [],
     'regional_office': []}
2014-06-11 23:20:44+0530 [movie] INFO: Closing spider (finished)
214-06-11 23:20:44+0530 [movie] INFO: Dumping Scrapy stats:
    {'downloader/request_bytes': 256,
     'downloader/request_count': 1,
     'downloader/request_method_count/GET': 1,
     'downloader/response_bytes': 6638,
     'downloader/response_count': 1,
     'downloader/response_status_count/200': 1,
     'finish_reason': 'finished',
     'finish_time': datetime.datetime(2014, 6, 11, 17, 50, 44, 54000),
     'item_scraped_count': 1,
     'log_count/DEBUG': 4,
     'log_count/INFO': 7,
     'response_received_count': 1,
     'scheduler/dequeued': 1,
     'scheduler/dequeued/memory': 1,
     'scheduler/enqueued': 1,
     'scheduler/enqueued/memory': 1,
     'start_time': datetime.datetime(2014, 6, 11, 17, 50, 43, 681000)}

2 个答案:

答案 0 :(得分:2)

除了@ Talvalin的回答,正确的XPath应该是以下形式:

item["movie_name"] = hxs.xpath("//*[@id='lblMovieName']/font/text()").extract()

出于某种原因,当页面加载时,<font>标记与<span>标记(或id所在的任何标记)分开。我已对此进行了测试,但确实有效。

警告的话:该网站几乎不受刮擦。我尝试过第二次刮,然后立即扔了Runtime Error

答案 1 :(得分:1)

应在没有http://的情况下定义允许的域。例如:

allowed_domains= ["cbfcindia.gov.in/"]

如果问题仍然存在,请显示完整日志,其中包含已抓取网页的详细信息以及可能发生的任何重定向。