网络爬虫基础练习

5.练习:

 

取出h1标签的文本

import requests

from bs4 import BeautifulSoup
url='http://localhost:63342/bd/rua.html?_ijt=u21vkhk5ujorshtgcta6r0t380'
res=requests.get(url)
res.encoding='utf-8'
# print(res.text)
soup=BeautifulSoup(res.text,'html')
print(soup.h1)

 


取出a标签的链接

print(soup.a)

 


取出所有li标签的所有内容

soup=BeautifulSoup(res.text,'html')
for i in soup.select('li'):
    print(i)

 


取出一条新闻的标题、链接、发布时间、来源

 

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)

 

posted @ 2018-03-29 11:38  126刘畅  阅读(103)  评论(0编辑  收藏  举报