网络爬虫基本练习

1:取出hi标签的文本

import requests
url = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0328/9113.html'
res = requests.get(url)
res.encoding='utf-8'
from bs4 import BeautifulSoup
soup = BeautifulSoup(res.text,'html.parser')
soup.h1.text

 2、取出a标签链接

soup.a.attrs.get('href')

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

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

 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)

 

posted @ 2018-03-29 17:00  090伍明航  阅读(144)  评论(0编辑  收藏  举报