【发布时间】:2015-10-19 05:13:09
【问题描述】:
我正在尝试从中国网站递归抓取数据。我让我的蜘蛛跟随“下一页”网址,直到没有“下一页”可用。下面是我的蜘蛛:
import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from hrb.items_hrb import HrbItem
class HrbSpider(CrawlSpider):
name = "hrb"
allowed_domains = ["www.harbin.gov.cn"]
start_urls = ["http://bxt.harbin.gov.cn/hrb_bzbxt/list_hf.php"]
rules = (
Rule(SgmlLinkExtractor(allow=(), restrict_xpaths=(u'//a[@title="\u4e0b\u4e00\u9875"]',)), callback="parse_items", follow= True),
)
def parse_items(self, response):
items = []
for sel in response.xpath("//table[3]//tr[position() > 1]"):
item = HrbItem()
item['id'] = sel.xpath("td[1]/text()").extract()[0]
title = sel.xpath("td[3]/a/text()").extract()[0]
item['title'] = title.encode('gbk')
item['time1'] = sel.xpath("td[3]/text()").extract()[0][2:12]
item['time2'] = sel.xpath("td[5]/text()").extract()[1]
items.append(item)
return(items)
问题在于它只抓取了前 15 页。我浏览了第 15 页,仍然有一个“下一页”按钮。那么它为什么停止了呢?网站是否打算防止抓取?还是我的代码有问题?如果我们一次只能抓取 15 页,有没有办法从某个页面开始抓取,比如第 16 页?非常感谢!
【问题讨论】: