当前位置:首页 > Web开发 > 正文

比如 ArrowLeft. 一个包含所有键名的列表见 USKeyboardLayout.- options objec

2024-03-31 Web开发

https://www.cnblogs.com/ruhai/p/11318499.html


requests-html模块

官方网站


Github网址


请求数据
from requests_html import HTMLSession

session = HTMLSession()

requests-html发出的请求是由session发出来的


发送Get请求


url = ‘https://baidu.com‘
res= session.get(url = url)

发送post请求


url = ‘https://baidu.com‘

res= session.post(url = url)

也可以使用request要领,,指定GET,或者是POST参数来指定,这个使用要领就和使用requests中的session要领类封装的


url = ‘‘
res = session.request(method = ‘GET‘,url = url)
print(res.html.html)

get要领和post还有request的要领和requests的模块中的要领一致,至于为什么,因为这两个模块是一小我私家写的


自界说HTML东西
from requests_html import HTML
doc = """<a href=http://www.mamicode.com/‘https:/httpbin.org‘>"""

html = HTML(html=doc)
print(html.links)
{‘https://httpbin.org‘}
HTML东西属性
url = ‘https://www.zhihu.com/signin?next=%2F‘
res = session.get(url = url)

在ipython中查抄res类型


In [15]: res
Out[15]: <Response [200]>
In [16]: type(res)
Out[16]: requests_html.HTMLResponse
In [17]: res.html
Out[17]: <HTML url=‘https://www.zhihu.com/signin?next=%2F‘>

In [18]: type(res.html)
Out[18]: requests_html.HTML

可以看到requests_html.HTML 和requests_html.HTMLResponse 模块本身实现的类


In [19]: dir(res.html)
Out[20]:
[‘absolute_links‘, ‘add_next_symbol‘, ‘arender‘, ‘base_url‘, ‘default_encoding‘, ‘element‘, ‘encoding‘, ‘find‘, ‘full_text‘, ‘html‘, ‘links‘, ‘lxml‘, ‘next‘, ‘next_symbol‘, ‘page‘, ‘pq‘, ‘raw_html‘, ‘render‘, ‘search‘, ‘search_all‘, ‘session‘, ‘skip_anchors‘, ‘text‘, ‘url‘, ‘xpath]

撤除失模块内部封装的属性和要领之外有这么多的要领和属性,下面我们一步一步类进行介绍。


html东西属性
absolute_links

输入页面中的绝对路径,如果页内连接是相对路径也会被自动转换为绝对路径


url = ‘https://www.zhihu.com/signin?next=%2F‘
res = session.get(url = url)

这里我们请求知乎首页,可以底部查抄网址,



In:res.html.absolute_links
Out:
{
‘https://www.zhihu.com/app‘,
‘https://www.zhihu.com/contact‘,
‘https://www.zhihu.com/explore‘,
‘https://www.zhihu.com/jubao‘,
‘https://www.zhihu.com/org/signup‘,
‘https://www.zhihu.com/question/waiting‘,
}

相对路径被转换为绝对路径


links

原样连接,页面中是绝对路径的输出就是绝对路径,是相对路径输出就是相对路径


In : res.html.links
Out:
{
‘/app‘,
‘/contact‘,
‘/explore‘,
‘/jubao‘,
‘https://www.zhihu.com/org/signup‘,
‘https://www.zhihu.com/term/privacy‘,
‘https://www.zhihu.com/terms‘,
‘}
base_url

? 根本连接


html

返回响应页面的html代码


raw_html

返回页面的二进制流


text

输入响应中所有的文字,功效如下,


In [21]: res.html.text
Out[21]: ‘知乎 - 有问题,上知乎\n.u-safeAreaInset-top { height: constant(safe-area-inset-top) !important; height: env(safe-
area-inset-top) !important; } .u-safeAreaInset-bottom { height: constant(safe-area-inset-bottom) !important; height: env(safe-area-inset-bottom) !important; }\nif (window.requestAnimationFrame) { window.requestAnimationFrame(function() { window.FIRST_ANIMATION_FRAME = Date.now(); }); }\n首页\n发明\n等你来答\n登录插手知乎\n有问题,上知乎\n免暗码登录\n暗码登录\n获取短
信验证码\n接收语音验证码\n注册/登录\n未注册手机验证后自动登录\n注册即代表同意《知乎协议》《隐私掩护指引》\n注册机构号\n社交
帐号登录\n微信\nQQ\nQQ\n微博\n下载知乎 App\n知乎专栏圆桌发明移动应用联系我们来知乎事情注册机构号\n? 2019 知乎京 ICP 证 1107
45 号京公网安备 11010802010035 号出版物经营许可证\n侵权举报网上有害信息举报专区儿童色情信息举报专区违法和不良信息举报:010-
82716601\n

encoding

字符编码


可以通过以下要领设置字符编码


res.html.encoding = ‘gbk‘
html东西要领
find

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32376.html