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
wisefree
V2EX  ›  Python

爬虫的时候发现一个有趣的现象,请问这是怎么回事?

  •  
  •   wisefree · 2017-03-09 12:09:31 +08:00 · 3405 次点击
    这是一个创建于 2605 天前的主题,其中的信息可能已经有所发展或是发生改变。

    爬取地址: https://tieba.baidu.com/p/4959928798 在 chrome 上查看源代码,有着一段

      <a class="pb_nameplate j_nameplate j_self_no_nameplate" href="/tbmall/propslist?category=112&ps=24" data-field='{&quot;props_id&quot;:&quot;1120050972&quot;,&quot;end_time&quot;:&quot;1512731564&quot;,&quot;title&quot;:&quot;\u6d77\u8d3c\u738b\u7684\u53f3\u624b&quot;,&quot;optional_word&quot;:[&quot;\u7684&quot;,&quot;\u4e4b&quot;,&quot;\u306e&quot;],&quot;pattern&quot;:[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;3&quot;]}' target="_blank">海贼王的右手</a>
    

    依据: class="pb_nameplate j_nameplate j_self_no_nameplate

    写了一个正则:(?<=pb_nameplate\sj_nameplate\sj_self_nameplate)[\s\S]*?(?=)

    运行后发现死活匹配不了,所以

    # -*- coding: utf-8 -*-
    __author__ = 'duohappy'
    
    import requests
    
    def get_info_from(url):
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
        }
    
        web_data = requests.get(url, headers=headers)
        web_data.encoding = 'utf-8'
        content = web_data.text
        
        with open('./test.txt', 'w') as f:
            f.write(content)
    
    if __name__ == '__main__':
        url = 'http://tieba.baidu.com/p/4959928798'
        
        get_info_from(url)
        
    

    才发现

    <a class="pb_nameplate j_nameplate j_self_nameplate" href="/tbmall/propslist?category=112&ps=24" data-field='{&quot;props_id&quot;:&quot;1120050972&quot;,&quot;end_time&quot;:&quot;1512731564&quot;,&quot;title&quot;:&quot;\u6d77\u8d3c\u738b\u7684\u53f3\u624b&quot;,&quot;optional_word&quot;:[&quot;\u7684&quot;,&quot;\u4e4b&quot;,&quot;\u306e&quot;],&quot;pattern&quot;:[&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;3&quot;]}' target="_blank">海贼王的右手</a>
    

    class="pb_nameplate j_nameplate j_self_no_nameplate 变成了 pb_nameplate j_nameplate j_self_nameplate

    这是什么技术,还是我的姿势有问题?

    12 条回复    2017-03-09 14:26:18 +08:00
    amustart
        1
    amustart  
       2017-03-09 12:31:51 +08:00   ❤️ 1
    正则- > 网页解析


    (源码的改变或许是因为你从 chrome 里看的和你真实爬到的不一致?)
    amustart
        2
    amustart  
       2017-03-09 12:32:28 +08:00
    @amustart 正则- > 网页解析器
    wisefree
        3
    wisefree  
    OP
       2017-03-09 12:34:15 +08:00
    @amustart 对,我爬到的网页代码,和直接在 chrome 查看网页源代码有差异,这个还是第一次遇到
    holyzhou
        4
    holyzhou  
       2017-03-09 12:39:08 +08:00   ❤️ 1
    呵呵 我刚试了下 , 应该是你网页登录了, 脚本没有登录。
    导出为 curl 命令行,可以对比一下带 cookie 内容跟不带 get 后的内容。
    954880786
        5
    954880786  
       2017-03-09 12:39:09 +08:00 via iPhone   ❤️ 1
    楼主试试把 headers 伪造的完整一点呢,也有可能是 js 动态执行的缘故
    ljcarsenal
        6
    ljcarsenal  
       2017-03-09 12:40:34 +08:00 via Android   ❤️ 1
    你是查看源代码 还是 f12 的检查元素
    annielong
        7
    annielong  
       2017-03-09 12:45:58 +08:00   ❤️ 1
    如果出问题一般都用笨方法,开始的时候先输出爬到的全文,根据爬到的全文做解析,而不是看网页
    wisefree
        8
    wisefree  
    OP
       2017-03-09 12:47:43 +08:00
    @holyzhou 确实如此,我退出贴吧账号后,再查看源代码,就没有问题!谢谢啦!厉害
    wisefree
        9
    wisefree  
    OP
       2017-03-09 12:48:07 +08:00
    @annielong 嗯嗯,一次一定会注意
    wisefree
        10
    wisefree  
    OP
       2017-03-09 12:49:02 +08:00
    原因被 @holyzhou 指出了,是登录的问题!
    谢谢大家关注
    QQ2112755791
        11
    QQ2112755791  
       2017-03-09 13:47:30 +08:00
    代码不错,会不会跟服务器有关呢?
    ChangHaoWei
        12
    ChangHaoWei  
       2017-03-09 14:26:18 +08:00
    用 firefox 或者 chrome 的时候记得装个 js 开关。这样你就能看到没有 js 修改 DOM 的界面效果了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2732 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:30 · PVG 10:30 · LAX 19:30 · JFK 22:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.