re 正则表达式 爬取网站标题
1 2 3 4 5 6 7 8 | import requests import re url = 'http://www.jd.com/' <br> #url='http://www.eastmoney.com/' r = requests.get(url) r.encoding = 'utf-8' data = re.findall( '<title>(.*?)</title>' ,r.text,re.S) print (data) |
['京东(JD.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import re # 提取python key = "javapythonc++php" re.findall( "python" , key)[ 0 ] """ python """ # 提取出hello world key = "<html><h1>hello world</h1></html>" re.findall( '<h1>hello world</h1>' , key) """ ['<h1>hello world</h1>'] """ # 提取170 string = "我喜欢身高为170的女孩" # re.findall("170", string)[0] re.findall( '\d+' , string) """ ['170'] """ # 提取出http://和https:// key = 'http://www.baidu.com and https://boob.com' re.findall( 'https{0,1}' , key) # {}前的字符出现0次或1次 """ ['http', 'https'] """ # 提取出hit. key = "bobo@hit.edu.com" re.findall( "h.*\." , key) # .表示任意字符(\n除外);*表示匹配0个或多个;\表示对.转义 """ ['hit.edu.'] """ # 贪婪模式:根据正则表达式尽可能多地提取数据。 # 切换为非贪婪模式,加一个"?" re.findall( "h.*?\." , key) """ ['hit.'] """ # 匹配sas和saas key = "saas and sas and saaas" re.findall( 'sa{1,2}s' , key) # 匹配1-2次由前面表达式定义的片段 """ ['saas', 'sas'] """ # 匹配i开头的行 re.S:基于单行匹配 re.M:基于多行匹配 string = '''fall in love with you i love you very much i love she i love her''' re.findall( "^i.*" , string, re.M) """ ['i love you very much', 'i love she', 'i love her'] """ # 匹配所有的行 string = """<div>静夜思 床前明月光 疑是地上霜 举头望明月 低头思故乡 </div>""" re.findall( '<div>.*</div>' , string, re.S) """ ['<div>静夜思\n床前明月光\n疑是地上霜\n举头望明月\n低头思故乡\n</div>'] """
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)