1.取出h1标签的文本
import requests re=requests.get(‘http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0328/9113.html‘) re.encoding=‘utf-8‘ print(re) print(re.text) print(soup.h1.text)
2.取出a标签的链接
soup.a.attrs.get(‘href‘)
3.取出所有li标签的所有内容
for i in soup.select(‘li‘): print(i.text)
4.取出一条新闻的标题、链接、发布时间、来源
print(soup.select(‘.news-list-title‘)[0].text) print(soup.select(‘li‘)[1].a.attrs[‘href‘]) print(soup.select(‘.news-list-info‘)[0].contents[0].text) print(soup.select(‘.news-list-info‘)[0].contents[1].text)