网络爬虫基础练习
5.练习:
取出h1标签的文本
print(soup.h1.text)
取出a标签的链接
for link in soup.select('a'): print(link.get('href'))
取出所有li标签的所有内容
for i in soup.select('li'): print(i.contents[1])
取出一条新闻的标题、链接、发布时间、来源
print(soup.select('.news-list-title')[0].text) print(soup.select('li')[0].a.attrs['href']) print(soup.select('.news-list-info')[0].contents[0].text) print(soup.select('.news-list-info')[0].contents[1].text)