关于ide:AutoScraper-让你的爬虫聪明起来

5次阅读

共计 2308 个字符,预计需要花费 6 分钟才能阅读完成。

【导语】: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 AutoScraper
url = '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 AutoScraper
url = '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 等。

正文完
 0