跳转到主要内容

一个用于自动化与网站交互的Python库

项目描述

MechanicalSoup. A Python library for automating website interaction.

主页

https://mechanicalsoup.readthedocs.io/

概览

一个用于自动化与网站交互的Python库。MechanicalSoup可以自动存储和发送cookies,遵循重定向,可以跟踪链接和提交表单。它不支持JavaScript。

MechanicalSoup由M Hickford创建,他曾是Mechanize库的忠实用户。不幸的是,Mechanize直到2019年才与Python 3兼容,并且其开发停滞了几年。MechanicalSoup提供了一个类似的API,基于Python巨头Requests(用于HTTP会话)和BeautifulSoup(用于文档导航)。自2017年以来,它是由包括@hemberger@moy在内的一个小团队积极维护的项目。

Gitter Chat

安装

Latest Version Supported Versions

PyPy3也受到支持(并进行测试)。

PyPI下载并安装最新发布的版本。

pip install MechanicalSoup

GitHub下载并安装开发版本。

pip install git+https://github.com/MechanicalSoup/MechanicalSoup

从源码安装(安装当前工作目录中的版本)

python setup.py install

(在所有情况下,将--user添加到install命令中,以便在当前用户的家目录中安装。)

文档

完整文档可在https://mechanicalsoup.readthedocs.io/找到。您可以直接跳转到自动生成的API文档

示例

examples/expl_qwant.py,获取Qwant搜索结果的代码

"""Example usage of MechanicalSoup to get the results from the Qwant
search engine.
"""

import re
import mechanicalsoup
import html
import urllib.parse

# Connect to Qwant
browser = mechanicalsoup.StatefulBrowser(user_agent='MechanicalSoup')
browser.open("https://lite.qwant.com/")

# Fill-in the search form
browser.select_form('#search-form')
browser["q"] = "MechanicalSoup"
browser.submit_selected()

# Display the results
for link in browser.page.select('.result a'):
    # Qwant shows redirection links, not the actual URL, so extract
    # the actual URL from the redirect link:
    href = link.attrs['href']
    m = re.match(r"^/redirect/[^/]*/(.*)$", href)
    if m:
        href = urllib.parse.unquote(m.group(1))
    print(link.text, '->', href)

更多示例可在examples/中找到。

对于更复杂的表单(复选框、单选按钮和文本区域)的示例,请阅读tests/test_browser.pytests/test_form.py

开发

Build Status Coverage Status Documentation Status CII Best Practices

关于构建、测试和为MechanicalSoup做出贡献的说明:请参阅CONTRIBUTING.rst

常见问题

阅读FAQ

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分布

MechanicalSoup-1.3.0.tar.gz (50.8 kB 查看哈希值)

上传时间

构建分布

MechanicalSoup-1.3.0-py3-none-any.whl (20.0 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面