python爬虫练习5——新闻联播

提取新闻联播相关文字并输出

网址:新闻联播 (cctv.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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('文件保存成功')

  

 

 

目前只能单条爬取,如何多条爬取输出,还望各位指点,另外,输出内容不能分行,不太美观,还希望各位改进指正

 

 

 

posted @   徐海建  阅读(459)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示