安裝
pip install requests_html
基本使用
from requests_html import HTMLSession,HTML
session = HTMLSession()
headers={
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
}
url = "https://www.baidu.com"
response = session.get(url=url,headers=headers)
session.post('http://httpbin.org/post', data={'name': 'zzz', 'passwd': 123})
response.html.html
for _ in response.html.links:
print(_)
for _ in response.html.absolute_links:
print(_)
response.html.find('#some a',first=True,_encoding='utf-8', clean=false)
response.html.xpath('//[@id="some"]/a')
ele.text
ele.html
ele.attrs.get('id')
e.links
e.absolute_links
生成html
doc = """
<!DOCTYPE html>
<html>
hello
</html>
"""
html = HTML(html=doc)
script支持
res = session.get('http://python-requests.org/')
res.html.render()
script ='''
console.log('script')
'''
html.render(script=script)