autoscraper

alirezamikaMIT活跃维护

给一个示例 URL,自动学习网页结构并生成抓取规则,省去手写解析逻辑,适合快速采集相似页面的数据。

A Smart, Automatic, Fast and Lightweight Web Scraper for Python

v1.1.14发布于 2022-07-17 17:19 UTC8,081star +5 / 7 天832fork同步于 2026-11-10 11:31 UTC

怎么上手

由 README 提炼,执行前请确认
pip install autoscraper
硬件要求按 README 说明

README 未说明,纯 Python 库,无需 GPU

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

给几个样例数据就能自动学出抓取规则的 Python 网页爬虫库,省去手写解析逻辑

给一个示例 URL,自动学习网页结构并生成抓取规则,省去手写解析逻辑,适合快速采集相似页面的数据。

核心能力

  • 用样例文本、URL 或标签值自动学习抓取规则
  • build 后可用 get_result_similar 抓同类内容
  • get_result_exact 按样例顺序取精确字段
  • 支持传入 requests 参数如代理和自定义请求头
  • 可将学好的模型 save/load 复用

容易踩的坑

示例中的 wanted_list 需按目标页面实际内容替换,页面动态变化时旧样例会失效;抓取依赖网络请求,需自行处理代理或请求头;README 未说明默认端口等运行配置

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v1.1.14
  1. v1.1.141.1.14最新正式版2022-07-17 · 2022-07-17 17:19 UTC

    这个版本没有写更新说明。

  2. v1.1.122021-01-23 · 2021-01-23 17:10 UTC

    这个版本没有写更新说明。

  3. v1.1.112021-01-10 · 2021-01-10 13:30 UTC

    这个版本没有写更新说明。

  4. v1.1.102020-11-29 · 2020-11-29 13:02 UTC

    这个版本没有写更新说明。

  5. v1.1.92020-11-05 · 2020-11-05 09:23 UTC

    这个版本没有写更新说明。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    AutoScraper: A Smart, Automatic, Fast and Lightweight Web Scraper for Python

    img

    This project is made for automatic web scraping to make scraping easy. It gets a url or the html content of a web page and a list of sample data which we want to scrape from that page. This data can be text, url or any html tag value of that page. It learns the scraping rules and returns the similar elements. Then you can use this learned object with new urls to get similar content or the exact same element of those new pages.

    Installation

    It's compatible with python 3.

    • Install latest version from git repository using pip:
    $ pip install git+https://github.com/alirezamika/autoscraper.git
    
    • Install from PyPI:
    $ pip install autoscraper
    
    • Install from source:
    $ python setup.py install
    

    How to use

    Getting similar results

    Say we want to fetch all related post titles in a stackoverflow page:

    from autoscraper import AutoScraper
    
    url = 'https://stackoverflow.com/questions/2081586/web-scraping-with-python'
    
    # We can add one or multiple candidates here.
    # You can also put urls here to retrieve urls.
    wanted_list = ["What are metaclasses in Python?"]
    
    scraper = AutoScraper()
    result = scraper.build(url, wanted_list)
    print(result)
    

    Here's the output:

    [
        '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?'
    ]
    

    Now you can use the scraper object to get related topics of any stackoverflow page:

    scraper.get_result_similar('https://stackoverflow.com/questions/606191/convert-bytes-to-a-string')
    

    Getting exact result

    Say we want to scrape live stock prices from yahoo finance:

    from autoscraper import AutoScraper
    
    url = 'https://finance.yahoo.com/quote/AAPL/'
    
    wanted_list = ["124.81"]
    
    scraper = AutoScraper()
    
    # Here we can also pass html content via the html parameter instead of the url (html=html_content)
    result = scraper.build(url, wanted_list)
    print(result)
    

    Note that you should update the wanted_list if you want to copy this code, as the content of the page dynamically changes.

    You can also pass any custom requests module parameter. for example you may want to use proxies or custom headers:

    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))
    

    Now we can get the price of any symbol:

    scraper.get_result_exact('https://finance.yahoo.com/quote/MSFT/')
    

    You may want to get other info as well. For example if you want to get market cap too, you can just append it to the wanted list. By using the get_result_exact method, it will retrieve the data as the same exact order in the wanted list.

    Another example: Say we want to scrape the about text, number of stars and the link to issues of Github repo pages:

    from autoscraper import AutoScraper
    
    url = 'https://github.com/alirezamika/autoscraper'
    
    wanted_list = ['A Smart, Automatic, Fast and Lightweight Web Scraper for Python', '6.2k', 'https://github.com/alirezamika/autoscraper/issues']
    
    scraper = AutoScraper()
    scraper.build(url, wanted_list)
    

    Simple, right?

    Saving the model

    We can now save the built model to use it later. To save:

    # Give it a file path
    scraper.save('yahoo-finance')
    

    And to load:

    scraper.load('yahoo-finance')
    

    Tutorials

    Issues

    Feel free to open an issue if you have any problem using the module.

    Support the project

    Happy Coding ♥️