学习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
保持对斗争的信仰!