【发布时间】:2017-01-15 12:15:48
【问题描述】:
我想抓取一个有两级url的网站,第一级是多页列表,url如下:
这样的页面布局:
- 列表项链接 1
- 列表项链接 2
- 列表项链接 3
- 列表项链接 4
1,2,3,4,5 ...下一页
第二层是详情页,url如下:
我的蜘蛛代码是:
import scrapy
from scrapy.spiders.crawl import CrawlSpider
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor
from scrapy.spiders.crawl import Rule
from urlparse import urljoin
class MyCrawler(CrawlSpider):
name = "AnjukeCrawler"
start_urls=[
"http://www.example.com/group/"
]
rules = [
Rule(LxmlLinkExtractor(allow=(),
restrict_xpaths=(["//div[@class='multi- page']/a[@class='aNxt']"])),
callback='parse_list_page',
follow=True)
]
def parse_list_page(self, response):
list_page=response.xpath("//div[@class='li- itemmod']/div/h3/a/@href").extract()
for item in list_page:
yield scrapy.http.Request(self,url=urljoin(response.url,item),callback=self.parse_detail_page)
def parse_detail_page(self,response):
community_name=response.xpath("//dl[@class='comm-l-detail float-l']/dd")[0].extract()
self.log(community_name,2)
我的问题是:我的 parse_detail_page 似乎从未运行过,有人可以告诉我为什么吗?我该如何解决?
谢谢!
【问题讨论】:
-
您确定您的 xpath 查询会生成结果吗?您可以使用
print获取项目列表。 -
是的,我已经调试了代码,请求已经创建
-
@hl79-james 哦,你能发布爬取日志吗?您可以通过
scrapy crawl myspider &> output.log生成一个。您的请求很可能被欺骗过滤器过滤。