美文网首页Web Scraper
【实践】笔记_Chrome插件webscraper爬取天眼通数据

【实践】笔记_Chrome插件webscraper爬取天眼通数据

作者: 大婶N72 | 来源:发表于2019-03-01 01:01 被阅读0次

    需求:获取指定关键字(母婴)下的公司信息(公司名称、地址、法人、联系方式)
    步骤:
    一、下载webscraper插件。
    通过Chrome浏览器的扩展程序发现发不开Chrome商店,此处提供一个百度云盘链接(https://pan.baidu.com/s/1E0DCxzGvz4qkaLin_i8L9w),将文件下载到本地
    二、安装webscraper插件到Chrome浏览器。
    打开Chrome浏览器的扩展程序页面(chrome://extensions/),将本地的插件文件拖动到浏览器的插件页面,按照提示操作便可,出现下图说明成功:

    插件安装成功

    三、启动webscraper插件
    打开Chrome浏览器,按下F12(有的是Fn+F12),在导航中单击web scraper
    四、webscrapy工作原理
    参考:https://www.yuanrenxue.com/tricks/web-scraper-principle-1.html

    结构
    运行逻辑

    五、开始抓取天眼通数据
    1.最快的方式就是导入现成的sitemap文件,导入到webscraper中即可,此处我提供一个已经写好的(结构还是很清晰的),读者在使用时可按照自己的需要修改关键字和爬取的页数:
    {"selectors":[{"parentSelectors":["_root"],"type":"SelectorElement","multiple":true,"id":"root","selector":"div.search-result-single","delay":"2000"},{"parentSelectors":["root"],"type":"SelectorLink","multiple":false,"id":"jump","selector":"a.name","delay":"800"},{"parentSelectors":["jump"],"type":"SelectorText","multiple":false,"id":"company","selector":"h1.name","regex":"","delay":"300"},{"parentSelectors":["jump"],"type":"SelectorText","multiple":false,"id":"phone","selector":"div.in-block:nth-of-type(1) span:nth-of-type(2)","regex":"","delay":"200"},{"parentSelectors":["jump"],"type":"SelectorText","multiple":false,"id":"address","selector":"table.table.-striped-col tr:nth-of-type(9) td:nth-of-type(2)","regex":"","delay":"400"},{"parentSelectors":["root"],"type":"SelectorText","multiple":false,"id":"Contacts","selector":"a.legalPersonName","regex":"","delay":"400"},{"parentSelectors":["jump"],"type":"SelectorText","multiple":false,"id":"scale","selector":"table.table.-striped-col tr:nth-of-type(7) td:nthf-type(4)","regex":"","delay":"200"}],"startUrl":"https://www.tianyancha.com/search/p[1-5]?key=银行&base=hefei","_id":"yinhang"}
    2.自己按照实际需要按照下面的基本步骤来创建

    步骤1 创建sitemap image.png image.png image.png image.png image.png

    以上只是最基本的操作,复杂的功能我目前用到了3个地方,在这里做下补充
    1.针对分页数据时,可以在访问的主入口中控制,比如我提供的现成sitemap中"startUrl":"https://www.tianyancha.com/search/p[1-5]?key=银行&base=hefei",这里面p[1-5]标识的是从第1到第5,间隔默认是1,那么webscraper就会依次访问1-5数字。
    2.涉及跳转新页面时,需要建立link,跳转的新页面不能另外打开,这里可以直接拷贝跳转页面的地址在当前页面打开,这样就可以继续在当前打开的webscraper中编写了。
    3.Delay时间建议都写,避免触发反爬虫
    4.webscraper使用的元素选择器是css selector。

    相关文章

      网友评论

        本文标题:【实践】笔记_Chrome插件webscraper爬取天眼通数据

        本文链接:https://www.haomeiwen.com/subject/mvsxuqtx.html