V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chinesehuazhou
V2EX  ›  Python

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

  •  
  •   chinesehuazhou · 2017-07-04 21:58:26 +08:00 · 1579 次点击
    这是一个创建于 2459 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 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)
            ......以下省略
    

    控制台输出:

    img

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

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

    BiggerLonger
        1
    BiggerLonger  
       2017-07-05 10:09:47 +08:00
    可能是 User-Agent 太舊了, 改成 10.0 試試?
    1130335361
        2
    1130335361  
       2017-07-05 10:47:51 +08:00
    你把 RotateUserAgentMiddleware 去掉试试,我这什么都没设置是好的,返回的还是 pc 端的地址
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5332 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:28 · PVG 15:28 · LAX 00:28 · JFK 03:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.