python爬虫练习5——新闻联播
提取新闻联播相关文字并输出
import requests import re url = 'https://tv.cctv.com/lm/xwlb/' ua = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.59'} r = requests.get(url , headers = ua ,timeout = 30) r.encoding = r.apparent_encoding pat1 = '<li><a href="(.*?)" target' url = re.compile(pat1,re.S).findall(r.text)[2] r = requests.get(url , headers = ua ,timeout = 30) r.encoding = r.apparent_encoding pat2 = 'description content="(.*?)<meta name' lst = re.compile(pat2,re.S).findall(r.text) print(lst) pat3 = '<p><strong>(.*?)</div>' body = re.compile(pat3,re.S).findall(r.text) print(body) path='C:/Users/Administrator/Desktop/xwlb.txt' with open (path,'w') as f: s = str(body) f.write(s) f.close() print('文件保存成功')
目前只能单条爬取,如何多条爬取输出,还望各位指点,另外,输出内容不能分行,不太美观,还希望各位改进指正