【发布时间】:2015-09-14 14:54:27
【问题描述】:
我尝试抓取的 HTML 代码格式错误:
<html>
<head>...</head>
<body>
My items here...
My items here...
My items here...
Pagination here...
</body>
</head>
</html>
问题是第二个</head>。我必须替换蜘蛛中的 HTML 才能使用 xpath 表达式:
class FooSpider(CrawlSpider):
name = 'foo'
allowed_domains = ['foo.bar']
start_urls = ['http://foo.bar/index.php?page=1']
rules = (Rule(SgmlLinkExtractor(allow=('\?page=\d',),),
callback="parse_start_url",
follow=True),)
def parse_start_url(self, response):
# Remove the second </head> here
# Perform my item
现在我想在我的规则中使用restrict_xpath 参数,但我不能,因为 HTML 格式错误:此时尚未执行替换。
请问你有什么想法吗?
【问题讨论】:
标签: python web-scraping scrapy