import requests
from bs4 import BeautifulSoup as bs
res = requests.get('http://news.sina.com.cn/c/zj/2018-04-20/doc-ifzihneq2559172.shtml')
res.encoding = 'utf-8'
soup = bs(res.text,'lxml')
t =[]
for p in soup.select('.article p'):
t.append(p.text.strip())
print('\n'.join(t))
就是随便点进新浪新闻的一篇新闻,在python3.5环境下爬出新闻的文本内容。
注意:
1)soup.select()函数中的article前面的'.'表示的是所有class = 'article',下的内容;若id为article,则要改成soup.select('# article p');
2)最后的输出形式根据自己的要求来。'\n'是对于每段article换行连接起来以str的形式输出。