使用 PySpider 爬取没有特征的网站 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
LeopardDennis
V2EX    问与答

使用 PySpider 爬取没有特征的网站

  • &nbs;
  •   LeopardDennis 2015-11-14 16:22:15 +08:00 2682 次点击
    这是一个创建于 3620 天前的主题,其中的信息可能已经有所发展或是发生改变。

    http://www.cers.edu.cn/cersii/instru.do?method=showInstru&instruId=1000100000003

    使用 PySpider 爬取上述网址的仪器信息,因为元素没有特征, css 选择器选择不到,所以想用 PyQuery 尝试选择到对应元素,第一次接触 Python 和 PyQuery ,代码如下,请各位帮忙指点一二,感谢。

    from pyspider.libs.base_handler import *
    import re
    from pyquery import PyQuery as pq
    from lxml import etree
    import urllib

    class Handler(BaseHandler):
    crawl_cOnfig= {
    }

    @every(minutes=24 * 60) def on_start(self): self.crawl('http://www.cers.edu.cn/cersii/instru.do?method=listInstru', callback=self.index_page) @config(age=10 * 24 * 60 * 60) def index_page(self, response): for each in response.doc('form a').items(): if re.search("showInstru", each.attr.href, re.U): self.crawl(each.attr.href, callback=self.detail_page) @config(priority=2) def detail_page(self, response): return { "仪器名称": , } 
    7 条回复    2016-03-08 23:24:51 +08:00
    xierch
        1
    xierch  
       2015-11-14 16:27:29 +08:00
    CSS selector 也有 :nth-child(),没有选不到的吧
    binux
        2
    binux  
       2015-11-15 16:35:00 +08:00
    写一个循环,选取所有的 td ,然后根据颜色判断是 key 还是 value ,拼装起来
    LeopardDennis
        3
    LeopardDennis  
    OP
       2015-11-15 17:07:25 +08:00
    @binux 我尝试了循环, pyquery 的 each 方法总报错,因为用到了 lembda ,声明的方法告诉我找不到,能否指点一下。
    binux
        4
    binux  
       2015-11-15 17:28:25 +08:00   1
    @LeopardDennis
    for each in doc('td') # 这样得到的是 lxml 对象
    或者 for each in doc('td').items() # 这样得到的依然是 pyquery 对象
    LeopardDennis
        5
    LeopardDennis  
    OP
       2015-11-22 23:58:17 +08:00
    @binux 您好,我在 demo.pyspider.org 上爬取了 8k+的数据,但是使用导出 csv 功能导出来的文件是空的,请问如何解决。项目地址: http://demo.pyspider.org/debug/eq_crawler
    binux
        6
    binux  
       2015-11-23 01:02:31 +08:00
    @LeopardDennis 导出的地址后面加上 ?offet=0&limit=100
    Izual
        7
    Izual  
       2016-03-08 23:24:51 +08:00
    css selector 应该很好写啊, a[href^="略
    另外还需要撸一个翻页的 callback 吧
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2835 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 39ms UTC 00:29 PVG 08:29 LAX 17:29 JFK 20:29
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86