我正在尝试从Yelp.com抓取一些信息。我正在使用Scrapy,我想从这个URL中提取一些信息:
http://www.yelp.com/search?find_desc=italiani+&find_loc=New+York%2C+NY%2C+USA&ns=1#find_desc=italian+restaurants&start=0
(搜索纽约的意大利餐馆)。
但我发现在这个页面的HTML代码中有以下几行:
<link href="http://www.yelp.com/search? find_desc=italiani&find_loc=New+York%2C+NY" rel="canonical">
因此,在抓取时,我会回顾规范页面的结果,它实际上与我指定的页面不对应。
如何使用Scrapy避免这个rel="canonical"
?
这里的代码(我使用了Scrapy的教程项目):
import scrapy
from tutorial.items import DmozItem
class DmozSpider(scrapy.Spider):
name = "dmoz"
allowed_domains = ["yelp.org"]
start_urls = [
#"http://www.yelp.com/search? find_desc=italiani+&find_loc=New+York%2C+NY%2C+USA&ns=1#find_desc=italian+restau rants&start=0"
"http://www.yelp.com/search?find_desc=italiani+&find_loc=New+York%2C+NY%2C+USA&ns=1#find_desc=italian+restaurants&start=10"
]
def parse(self, response):
filename = "response_body"
with open(filename, 'wb') as f:
f.write(response.body)
for sel in response.xpath('//div/h3/span'):
item = DmozItem()
item['title'] = sel.xpath('a/text()').extract()
item['link'] = sel.xpath('a/@href').extract()
item['desc'] = sel.xpath('text()').extract()
yield item