这是我的工作代码:
from scrapy.item import Item, Field
class Test2Item(Item):
title = Field()
from scrapy.http import Request
from scrapy.conf import settings
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
class Khmer24Spider(CrawlSpider):
name = 'khmer24'
allowed_domains = ['www.khmer24.com']
start_urls = ['http://www.khmer24.com/']
USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.97 Safari/537.22 AlexaToolbar/alxg-3.1"
DOWNLOAD_DELAY = 2
rules = (
Rule(SgmlLinkExtractor(allow=r'ad/.+/67-\d+\.html'), callback='parse_item', follow=True),
)
def parse_item(self, response):
hxs = HtmlXPathSelector(response)
i = Test2Item()
i['title'] = (hxs.select(('//div[@class="innerbox"]/h1/text()')).extract()[0]).strip(' \t\n\r')
return i
它只能抓取10或15条记录,而且总是随机的数字!我无法获取所有具有以下模式的页面:http://www.khmer24.com/ad/any-words/67-anynumber.html。我非常怀疑Scrapy由于重复请求而完成了爬虫。他们建议使用
dont_filter = True
,但我不知道在我的代码中放置它的位置。我是Scrapy的新手,真的需要帮助。