学习requests_html
一、获取页面上的所有链接。
from requests_html import HTMLSession session=HTMLSession() r=session.get('https://news.baidu.com/') #获取页面上的所有链接 all_links=r.html.links print(all_links) #获取页面上的所有链接,以绝对路径的方式 all_absolute_links=r.html.absolute_links print(all_absolute_links)
二、获取页面上的所有标题及其链接
from requests_html import HTMLSession session=HTMLSession() r=session.get('https://www.cnblogs.com/') news=r.html.find('#post_list > div:nth-child(n) > div.post_item_body > h3 > a') for new in news: print(new.text) print(new.absolute_links)
三、通过css选择器选取一个Element对象
>>> about = r.html.find('#about', first=True)
四、获取一个Element对象内的文本内容
>>> print(about.text)
五、获取一个Element对象的所有属性attributes
>>> about.attrs {'id': 'about', 'class': ('tier-1', 'element-1'), 'aria-haspopup': 'true'}
六、渲染出一个Element对象的HTML内容
>>> about.html
保持对斗争的信仰!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步