# Python #scrapy PC 端访问却变成移动端地址问题

2017-07-04 21:58:26 +08:00
 chinesehuazhou

用 scrapy 爬取喜马拉雅,爬取 PC 端地址,入口链接的 response 没问题,但后面 response 看到的是移动端地址。。。。

spider 代码如下:

class SpxmlySpider(scrapy.Spider):
    name = 'ximalaya'
    allowed_domains = ["ximalaya.com"]
    # 保存每页链接
    start_urls = ['http://www.ximalaya.com/dq/all/{}'.format(num) for num in range(2, 3)]  #先改为第二页试试

    def parse(self, response):
        # 取出专辑链接
        print(response)
        mainurls = response.xpath('//div[@class="albumfaceOutter"]/a/@href').extract()
        # for url in mainurls:
        #     yield Request(url = url, callback=self.parse_details)
        print(mainurls[0])
        yield Request(url = mainurls[0], dont_filter=True, callback = self.parse_details)

# TODO  为什么 PC 端访问会变成移动地址问题!!!!!!!!!!!!!!!!!
    def parse_details(self, response):
        item = XimalayaItem()
        print(response)
        ......以下省略

控制台输出:

已经写了一个 middlewares.RotateUserAgentMiddleware,给 headers 设置 user-agent,是生效的,输出内容也可以看出来。

是不是触发什么反爬机制?

1589 次点击
所在节点    Python
2 条回复
BiggerLonger
2017-07-05 10:09:47 +08:00
可能是 User-Agent 太舊了, 改成 10.0 試試?
1130335361
2017-07-05 10:47:51 +08:00
你把 RotateUserAgentMiddleware 去掉试试,我这什么都没设置是好的,返回的还是 pc 端的地址

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/373058

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX