python-httplib2

学习链接:https://httplib2.github.io/httplib2/

使用编写工具:notepad++

自我学习目的:使用httplib2获取获取想要的网页数据,再整理形成表格,提高效率


httplib2.content:获取访问网页的HTML内容

import httplib2
h = httplib2.Http(".cache")
(httplib2.resp_headers, httplib2.content) = h.request("http://example.org/","GET")
print("响应内容", httplib2.content)

 访问 http://example.org/

 将获取到的相应内容存储到example.html文件(在哪里打开的命令行,文件就在那个目录下)

import httplib2
h = httplib2.Http(".cache")
(httplib2.resp_headers, httplib2.content) = h.request("http://example.org/","GET")
filename = "example.html"
with open(filename, "w") as file:
    file.write(str(httplib2.content))

 b'<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title>\n\n    <meta charset="utf-8" />\n    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n    <meta name="viewport" content="width=device-width, initial-scale=1" />\n    <style type="text/css">\n    body {\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n        \n    }\n    div {\n        width: 600px;\n        margin: 5em auto;\n        padding: 2em;\n        background-color: #fdfdff;\n        border-radius: 0.5em;\n        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n    }\n    a:link, a:visited {\n        color: #38488f;\n        text-decoration: none;\n    }\n    @media (max-width: 700px) {\n        div {\n            margin: 0 auto;\n            width: auto;\n        }\n    }\n    </style>    \n</head>\n\n<body>\n<div>\n    <h1>Example Domain</h1>\n    <p>This domain is for use in illustrative examples in documents. You may use this\n    domain in literature without prior coordination or asking for permission.</p>\n    <p><a href="https://www.iana.org/domains/example">More information...</a></p>\n</div>\n</body>\n</html>

 httplib2.resp_header:请求头响应头

import httplib2
h = httplib2.Http(".cache")
(httplib2.resp_headers, httplib2.content) = h.request("http://example.org/","GET")
print("", httplib2.resp_headers)

执行结果:

 {'status': '200', 'age': '319629', 'cache-control': 'max-age=604800', 'content-type': 'text/html; charset=UTF-8', 'date': 'Mon, 21 Oct 2024 11:31:05 GMT', 'etag': '"3147526947+gzip"', 'expires': 'Mon, 28 Oct 2024 11:31:05 GMT', 'last-mo
dified': 'Thu, 17 Oct 2019 07:18:26 GMT', 'server': 'ECAcc (sac/2505)', 'vary': 'Accept-Encoding', 'x-cache': 'HIT', 'content-length': '1256', '-content-encoding': 'gzip', 'content-location': 'http://example.org/', '-varied-accept-encodi
ng': 'gzip, deflate'}
 

 ?httplib2.resp:

import httplib2
h = httplib2.Http(".cache")
h.add_credentials("name", "password")
(httplib2.resp, httplib2.content) = h.request("http://example.org/chapter/2",
                                              "PUT", body="This is text",
                                              headers={'content-type':'text/plain'} )
print("?响应次数", httplib2.resp)
print("响应内容", httplib2.content)

执行结果:

?响应次数 {'content-type': 'text/html; charset=UTF-8', 'date': 'Mon, 21 Oct 2024 12:13:11 GMT', 'server': 'ECAcc (sac/252D)', 'content-length': '0', 'status': '405'}
响应内容 b''

 headers={'CACHE-control':'no-cache'}

 第一个请求将被缓存,此后对该URI 的任何 GET 请求都将返回来自磁盘缓存的值,并且不会向服务器发出请求。

第二个请求添加了 Cache-Control:标头和“no-cache”值,告诉库在处理此请求时不得使用缓存的副本。

import httplib2
h = httplib2.Http(".cache")
(httplib2.resp, httplib2.content) = h.request("http://bitworking.org", "GET")
print("没有hearders参数信息. {}", httplib2.resp)
print("没有hearders参数信息:content:{}".format(httplib2.content))
(httplib2.resp, httplib2.content) = h.request("http://bitworking.org", "GET",
                                              headers={'CACHE-control':'no-cache'})
print("有hearders参数信息. {}", httplib2.resp)
print("有hearders参数信息:content:{}".format(httplib2.content))

执行结果:

import httplib2
h = httplib2.Http(".cache")
(httplib2.resp, httplib2.content) = h.request("http://bitworking.org", "GET")
print("没有hearders参数信息. {}", httplib2.resp)
print("没有hearders参数信息:content:{}".format(httplib2.content))

(httplib2.resp, httplib2.content) = h.request("http://bitworking.org", "GET")
print("没有hearders参数信息. {}", httplib2.resp)
print("没有hearders参数信息:content:{}".format(httplib2.content))

(httplib2.resp, httplib2.content) = h.request("http://bitworking.org", "GET",
                                              headers={'CACHE-control':'no-cache'})
print("有hearders参数信息. {}", httplib2.resp)
print("有hearders参数信息:content:{}".format(httplib2.content))

执行结果

实战1:访问荣耀X50i+ - 11.11全程1.2倍价保,退换货免运费 | 荣耀商城获取手机信息

import httplib2
h = httplib2.Http()
httplib2.content = h.request("https://www.honor.com/cn/shop/product/10086041939069.html", "GET")
print(httplib2.content)

? 执行结果:报错

 

XXXXXXXXXXXXXXXXX
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)

import httplib2
h = httplib2.Http()
h.ca_certs = False          # 去除SSL验证
h.disable_ssl_certificate_validation=True           #禁用SSL验证证书为真,不执行SSL证书验证
(httplib2.response, httplib2.content)= h.request("https://www.honor.com/cn/shop/product/10086041939069.html", "POST")
print(httplib2.content)

?执行结果:

b''

 访问地址:荣耀Magic6 Pro参数配置-规格性能 | 荣耀官方网站

import httplib2
h = httplib2.Http()
h.ca_certs = False          # 去除SSL验证
h.disable_ssl_certificate_validation=True           #禁用SSL验证证书为真,不执行SSL证书验证
(httplib2.response, httplib2.content)= h.request("https://www.honor.com/cn/phones/honor-magic6-pro/spec/", "POST")
print(httplib2.content)

执行结果:

### Ansible 2.9.3-1.el7 的依赖关系及提供者 Ansible 是一种简单而强大的自动化工具,用于配置管理、应用部署以及任务编排。对于版本 `Ansible 2.9.3-1.el7`,其依赖项主要由 RPM 包管理系统定义,并基于 CentOS 或 RHEL 7 平台上的软件仓库解析。 以下是该版本的主要依赖项及其可能的提供者: #### 主要依赖项 1. **Python >= 2.6**: Python 解释器是运行 Ansible 所必需的核心组件[^1]。 - 提供者:`python27` 或更高版本 (具体取决于操作系统中的默认安装)。 2. **Jinja2 >= 2.8**: Jinja2 是一个现代且功能丰富的模板引擎,广泛应用于 Ansible 中的任务和 Playbook 定义文件中[^2]。 - 提供者:通过 `python-jinja2` RPM 包实现支持。 3. **PyYAML**: PyYAML 库负责 YAML 文件的解析与序列化操作,在处理 Ansible 配置文件时至关重要[^3]。 - 提供者:RPM 包名为 `pyyaml` 或类似的变体名称。 4. **paramiko`: Paramiko 是 SSHv2 协议的一个纯 Python 实现,它使得远程服务器连接变得容易并安全[^4]。 - 提供者:通常来自 `python-paramiko` 软件包。 5. **setuptools`: Setuptools 是 Python 开发环境下的构建工具集之一,帮助完成模块打包等工作流程[^5]。 - 提供者:对应于 `python-setuptools` 这一标准库集合。 6. **cryptography`: Cryptography 提供低级加密原语访问接口,增强数据传输安全性[^6]。 - 提供者:一般情况下会预装有 `python-cryptography`. 7. **six`: Six 模块兼容 Python 2Python 3 编程风格差异问题解决方案[^7]。 - 提供者:可通过 `python-six` 获取相应资源。 8. **selinux-python`: SELinux 政策绑定到 Python API 上面以便更好地控制 Linux 系统权限分配机制[^8]。 - 提供者:如果启用了强制模式,则需额外引入此扩展特性;否则可选忽略不计。 以上列举的是典型场景下所需的直接关联项目清单,实际环境中还可能存在其他间接性的子需求链路情况发生改变的可能性存在,请参照官方文档或者本地 Yum/DNF 查询命令进一步确认最终状态信息详情如下所示: ```bash $ repoquery --requires ansible-2.9.3-1.el7.noarch | sort | uniq ``` 上述脚本能够有效展示目标程序所声明的一切外部条件约束列表形式呈现出来便于后续分析判断工作开展顺利推进下去。 ```python import subprocess def get_ansible_dependencies(package_name="ansible-2.9.3-1.el7"): try: result = subprocess.run( ["repoquery", "--requires", package_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) dependencies = sorted(set(result.stdout.splitlines())) return "\n".join(dependencies) except Exception as e: return f"Error occurred: {e}" print(get_ansible_dependencies()) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值