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

Scrapy 爬取页面时 xpath 取到数据不正确,是不是缓存的关系?

  •  
  •   tongle ·
    tongle · 2015-02-04 19:01:39 +08:00 · 5206 次点击
    这是一个创建于 3376 天前的主题,其中的信息可能已经有所发展或是发生改变。
    使用scrapy抓取appanie数据,有如下问题。
    2015-02-04 18:47:59+0800 [annie] DEBUG: Crawled (200) <GET http://www.appannie.com/apps/ios/top/china/games/?device=iphone>; (referer: None)
    Feb 3, 2015
    xpath取的格式如下



    预期的正确rank_date应该为 Feb 4, 2015
    这段代码之前跑了几个月都没有什么问题,今天忽然出现的,调整了setting文件设置了
    HTTPCACHE_ENABLED = False
    DOWNLOADER_STATS = False

    但是依然无法返回正常结果 有任何人遇到过这个问题,或者指出问题出在哪里吗?
    19 条回复    2015-02-05 13:25:14 +08:00
    Melodic
        1
    Melodic  
       2015-02-04 19:05:58 +08:00
    打开是个404,xpath中的第一个标签就搜不到,跑啥
    Melodic
        2
    Melodic  
       2015-02-04 21:09:05 +08:00   ❤️ 1
    不好意思,上面那个回答太草率了,下班之后我也爬了一下,一开始总是503

    2015-02-04 20:53:38+0800 [apple] DEBUG: Crawled (200) <GET http://www.appannie.com/apps/ios/top/china/games/?device=iphone> (referer: None)
    Feb 4, 2015

    后来比对了一下原因基本锁定在了user_agent的上面,一定是还带着scrapy的头就get去了

    给你提供了几个普通的浏览器user_agent ,放在settings.py 中。你再试一下吧

    #USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.54 Safari/536.5'
    tongle
        3
    tongle  
    OP
       2015-02-04 22:07:10 +08:00
    @Melodic USER_AGENT在settings.py里设置了五个 每次抓取的时候随机选取了其中一个的,然后是使用了proxy抓取的 会是这个的问题吗?
    Melodic
        4
    Melodic  
       2015-02-04 22:59:41 +08:00
    @tongle 可是我刚才抓成功了啊。开始以为是时间间隔的问题,后来发现取消延迟和随机延迟之后,仍然不能抓,只要换user agent了,就可以抓了,都是200,print有,存能存,何况爬虫,东西都拿下来了,怎么会存不了。

    帮你抓个大触过来0 0 @binux
    binux
        5
    binux  
       2015-02-04 23:25:04 +08:00   ❤️ 1
    你需要把失败时的页面存下来。
    tongle
        6
    tongle  
    OP
       2015-02-05 10:11:50 +08:00
    @Melodic 页面是抓下来了 返回的状态码也是200 但是信息和我直接访问看到的源码是不一致的,日期不对,抓取到的页面显示出来的 rank_date源信息确实是Feb 3, 2015,但是浏览器内查看到的源码是Feb 5, 2015

    @binux 页面存下来是要查看源信息是吗

    2015-02-05 10:03:32+0800 [annie] DEBUG: Crawled (200) <GET http://www.appannie.com/apps/ios/top/china/games/?device=iphone> (referer: None)
    [u'<div class="ss-top-chart-filter">\n \n <div class="filter-item" control-filter="list-picker" data-name="device" data-ganame="Device" data-title="\u8bbe\u5907" data-icon="icon-tablet"><a class="current">iPhone</a><a class="more"><i class="icon-caret-down"></i></a><a class="title"><span class="icon-tablet"></span><span class="filter-name">\u8bbe\u5907</span></a></div>\n <div class="filter-item" control-filter="tab-picker" data-name="country" data-ganame="Country" data-title="\u56fd\u5bb6" data-icon="icon-globe"><a class="current">\u4e2d\u56fd</a><a class="more"><i class="icon-caret-down"></i></a><a class="title"><span class="icon-globe"></span><span class="filter-name">\u56fd\u5bb6</span></a></div>\n <div class="filter-item" control-filter="menu-picker" data-name="category" data-ganame="Category" data-type="menu" data-title="\u7c7b\u522b" data-icon="icon-folder-open-alt"><a class="current">\u6e38\u620f</a><a class="more"><i class="icon-caret-down"></i></a><a class="title"><span class="icon-folder-open-alt"></span><span class="filter-name">\u7c7b\u522b</span></a></div>\n <div class="filter-item" control-filter="list-picker" data-name="iap" data-ganame="IAP" data-title="App\u5185\u8d2d\u4e70" data-icon="aaicon-inapppurchase" style=""><a class="current">\u6240\u6709App</a><a class="more"><i class="icon-caret-down"></i></a><a class="title"><span class="aaicon-inapppurchase"></span><span class="filter-name">App\u5185\u8d2d\u4e70</span></a></div>\n <div class="filter-item" control-filter="daily" data-name="date" data-ganame="Date" data-title="\u65e5\u671f" data-icon="icon-calendar"><a class="current">Feb 3, 2015</a><a class="more"><i class="icon-caret-down"></i></a><a class="title"><span class="icon-calendar"></span><span class="filter-name">\u65e5\u671f</span></a></div>\n \n </div>']

    这是刚刚我获取的页面,奇怪的就是<a class="current">Feb 3, 2015</a> 为什么还是Feb 3呢
    tongle
        7
    tongle  
    OP
       2015-02-05 10:29:44 +08:00
    @Melodic
    @binux
    确定是使用proxy的问题,解决办法是换proxy。
    roricon
        8
    roricon  
       2015-02-05 12:47:49 +08:00



    借道问@binux pyspider里面的age是什么时候生效的呢?我设置了10天的age后来改成1天,似乎任务还是会被scheduler忽略啊……
    roricon
        9
    roricon  
       2015-02-05 12:49:59 +08:00   ❤️ 1
    @tongle
    代理可以试下这个…… http://scrapinghub.com/crawlera/
    连UA都帮你一并搞定了的一站式服务。
    binux
        10
    binux  
       2015-02-05 12:52:25 +08:00
    @roricon 你是怎么确认 "任务还是会被scheduler忽略" 的,任务的当前状态是什么。修改后有没有尝试重启 pyspider 。
    roricon
        11
    roricon  
       2015-02-05 12:54:08 +08:00
    我在日志里面看到on_start之后,新进来的任务直接就被ignore了。
    我没重启pyspider,这个情况我是应该重启scheduler还是fetcher?或是其他组件?
    roricon
        12
    roricon  
       2015-02-05 12:54:36 +08:00
    @binux
    老忘记@ 真头痛
    binux
        13
    binux  
       2015-02-05 12:58:33 +08:00
    @roricon 先重启 processor 试试,不行 重启 scheduler
    roricon
        14
    roricon  
       2015-02-05 12:59:49 +08:00
    @binux 谢谢。顺便抱怨一下我的PR被Close感觉不会再爱了。T-T
    binux
        15
    binux  
       2015-02-05 13:04:05 +08:00
    @roricon proxy authorization 吗?你改变行为了,导致 CI 测试不通过。现在 proxy 是默认启用,你没有 merge 这个特性。
    binux
        16
    binux  
       2015-02-05 13:04:26 +08:00
    @roricon 解决后告诉我你重启了什么。
    roricon
        17
    roricon  
       2015-02-05 13:08:37 +08:00
    @binux 完全不懂CI,只看到我这边跑的很开心的功能它测试不通过。
    重启时候手滑不小心 supervisorctl reload 全部重启,然后问题解决了。
    binux
        18
    binux  
       2015-02-05 13:14:33 +08:00
    @roricon 有留下日志吗?我想看下日志。
    roricon
        19
    roricon  
       2015-02-05 13:25:14 +08:00
    @binux 日志发你邮箱了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2299 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 03:06 · PVG 11:06 · LAX 20:06 · JFK 23:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.