获取新浪新闻的新闻内容
因为新浪新闻里的新闻是放置在p标签内的,所以获取P标签的内容,
import urllib.request
import re
from bs4 import BeautifulSoup
from distutils.filelist import findall
#浏览地址,获取html内容
url='https://news.sina.com.cn/gov/xlxw/2019-12-06/doc-iihnzhfz3978738.shtml'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'}
page1=urllib.request.Request(url,headers=headers)
page=urllib.request.urlopen(page1)
contents = page.read()
soup = BeautifulSoup(contents,"html.parser")
list=[]#保存数据的数组
for t in soup.find_all('p'): # for循环遍历所有p标签,并把返回列表中的内容赋给t
print(t.get_text().strip("").strip('\r\n').replace(u'\u3000', u' ').replace(u'\xa0', u' ')+"\n")#去特殊符号
list.append( t.get_text().strip("").strip('\r\n').replace(u'\u3000', u' ').replace(u'\xa0', u' ')) #去特殊符号
file=open('data.txt','w',encoding='UTF-8-sig')#保存为txt
file.write(str(list)); ""
file.close()