【导语】:AutoScraper是一个智能、主动、疾速和轻量级的Web爬虫,应用简略便捷,让你从此辞别爬虫手动解析网页和写规定的懊恼。
简介
AutoScraper 是应用 Python 实现的 Web 爬虫,兼容 Python 3,能疾速且智能获取指定网站上的数据,这些数据能够是网页文本、URL 地址或者是其它 HTML 元素。另外,它还能够学习抓取规定并返回相似的元素。
下载安装
我的项目的源码地址是:
https://github.com/alirezamik...
兼容 Python 3。可应用以下办法进行装置:
(1)从git获取装置
$ pip install git+https://github.com/alirezamika/autoscraper.git
(2)从PyPI获取装置
$ pip install autoscraper
(3)下载源码后进行装置
$ python setup.py install
简略应用
假如咱们想在stackoverflow页面中获取所有相干的文章题目:
from autoscraper import AutoScraperurl = 'https://stackoverflow.com/questions/2081586/web-scraping-with-python'wanted_list = ["How to call an external command?"]scraper = AutoScraper()result = scraper.build(url, wanted_list)print(result)
输入后果如下:
[ 'How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?', 'How to call an external command?', 'What are metaclasses in Python?', 'Does Python have a ternary conditional operator?', 'How do you remove duplicates from a list whilst preserving order?', 'Convert bytes to a string', 'How to get line count of a large file cheaply in Python?', "Does Python have a string 'contains' substring method?", 'Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?']
抓取类似后果
当你还想获取stackoverflow上其余页面中所有相干的文章题目,则能够间接通过get\_result\_similar办法获取:
scraper.get_result_similar('https://stackoverflow.com/questions/606191/convert-bytes-to-a-string')
两个页面的抓取后果为:
抓取确切后果
当你只想抓取某个确切的后果,能够应用get\_result\_exact办法,即从wanted\_list中以完全相同的程序检索数据:
scraper.get_result_exact('https://stackoverflow.com/questions/606191/convert-bytes-to-a-string')
比方抓取页面中排第2的相干文章题目,执行后果:
自定义申请模块参数
你还能够传递任何自定义申请模块参数。例如,你可能想应用代理或自定义头:
proxies = { "http": 'http://127.0.0.1:8001', "https": 'https://127.0.0.1:8001',}result = scraper.build(url, wanted_list, request_args=dict(proxies=proxies))
抓取多项信息
假如咱们想要抓取对于文本,Star的数量和Github回购页面的问题链接:
from autoscraper import AutoScraperurl = 'https://github.com/alirezamika/autoscraper'wanted_list = ['A Smart, Automatic, Fast and Lightweight Web Scraper for Python', '2.5k', 'https://github.com/alirezamika/autoscraper/issues']scraper = AutoScraper()scraper.build(url, wanted_list)
执行后果为:
保留模型
咱们能够保留抓取的模型以便当前应用:
# 指定保留的文件门路scraper.save('stackoverflow')# 调用办法:scraper.load('stackoverflow')
AutoScraper简略介绍就到这里了,如果你想应用更多的性能,详见官方主页理解。
开源前哨
日常分享热门、乏味和实用的开源我的项目。参加保护 10万+ Star 的开源技术资源库,包含:Python、Java、C/C++、Go、JS、CSS、Node.js、PHP、.NET 等。