1、下载安装

pip install requests-html

2、使用

import requests_html
req = requests_html.HTMLSession()
responses = req.get(url)
# print(responses.text)
# print(responses.content)
print(responses.html.html)

支持javascript加载,第一次使用需要下载一个Chromium
然后就可以实现对一些使用js生成数据元素的获取了
responses.html.render()

支持css选择和xpath解析
rs=responses.html.find("#profile_block", first=True)
rs=responses.html.xpath('//a/text()')
获取元素之后,可以获取该元素的属性或者text值
rs.attrs获取属性
rs.text获取值


支持获取网页内链接,和绝对链接
print(responses.absolute_links)
print(responses.links)


支持获取下一页的地址,不过有的网站不支持
for url in responses.html:
    print(url)
responses.html.next()#获取下一页地址,有的网站不支持

其他使用方式和requests库一样,例如get请求和post

 

更多推荐

爬虫中requests-html包,作为requests的升级包,支持js加载