如何避免在爬行时被rel=canonical标签重定向?

2024-05-10 13:14:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从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代码中有以下一行:

^{pr2}$

所以,在爬行时,我收到规范页面的结果,它实际上与我指定的页面不符。
我怎样才能避免这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

Tags: comhttpnewsearchresponsewwwfinditem