事故:
今天写vpnbook.py的时候(参见),遇到一个问题,匹配到太多的数据,而且是我不需要的。
我要对某个html进行解析,又为了跨平台和快速使用,就没有使用第三方库(比如BeautifulSoup)
获得的html如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21Free VPN Accounts • 100% Free PPTP and OpenVPN Service 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 66 67 68 69 84 85 86 87 88 89 90 91 92 9394 95 96 97384 385 386 387 388 389 390 39198 99 150 151 152 153100 101136 137 138 139102 103 104 105 106 107 108 109 110 111
112 113 114 115 134 135140 141 142 143 144 145 146 147148 149154 155 380 381 382 383156 157 158 159378 379160 161179 180 181 182Free VPN
162 163PPTP and OpenVPN Accounts
164 165 166 167168 176 177178183 184235 236 237 238 239 240185 186 187 188193 194Free PPTP VPN
189 190 $0/mo191 192195 196 197 198 199 200 201 202203 204PPTP (point to point tunneling) is widely used since it is supported across all Microsoft Windows, 205 206 Linux, Apple, Mobile and PS3 platforms. It is however easier to block and might not work if your ISP or 207 208 government blocks the protocol. In that case you need to use OpenVPN, which is impossible to detect or block.
209 210 211 212213 214
227 228 229 230- euro195.vpnbook.com
215 216- euro213.vpnbook.com
217 218- uk180.vpnbook.com (UK VPN - optimized for fast web surfing; no p2p downloading)
219 220- us1.vpnbook.com (US VPN - optimized for fast web surfing; no p2p downloading)
221 222- Username: vpnbook
223 224- Password: bRudre3u
225 226More servers coming. Please Donate.231 232 233 234241 242295 296 297 298243 244 245 246251 252 253 254Free OpenVPN (Recommended)
247 248 $0/mo249 250255 256 257 258 259 260 261 262263 264 265 266OpenVPN is the best and most recommended open-source VPN software world-wide. It is the most secure VPN option. 267 268 You need to download the open-source OpenVPN Client and our configuration and certificate bundle 269 270 from the links below (use TCP if you cannot connect to UDP due to network restriction).
271 272 273 274275 276
291 292 Choose an OpenVPN Server from above293 294- Euro1 OpenVPN Certificate Bundle
277 278- Euro2 OpenVPN Certificate Bundle
279 280- UK OpenVPN Certificate Bundle (optimized for fast web surfing; no p2p downloading)
281 282- US OpenVPN Certificate Bundle (optimized for fast web surfing; no p2p downloading)
283 284- All bundles include UDP53, UDP 25000, TCP 80, TCP 443 profile
285 286- Username: vpnbook
287 288- Password: bRudre3u
289 290299 300357 358 359 360301 302 303 304309 310Donate
305 306 307 308311 312 313 314 315 316 317 318319 320 321 322323 324 325 326327 328 329 330331 332 333 334 Bitcoin Donation335 336 337 338 1FFExjn6sm2oMZ2LJsTtn1t8uXW6EE7HQ7339 340341 342 343 344 353 354 355 356361 362363 371 374375 376 377
我真正需要的东西很少,如下:
- euro195.vpnbook.com
- euro213.vpnbook.com
- uk180.vpnbook.com (UK VPN - optimized for fast web surfing; no p2p downloading)
- us1.vpnbook.com (US VPN - optimized for fast web surfing; no p2p downloading)
- Username: vpnbook
- Password: bRudre3u
刚开始的时候我匹配 <ul class="disc">.*</ul>,正则表达式如下
m = re.search('
- .*',html,re.S)
匹配到内容却是
- euro195.vpnbook.com
- euro213.vpnbook.com
- uk180.vpnbook.com (UK VPN - optimized for fast web surfing; no p2p downloading)
- us1.vpnbook.com (US VPN - optimized for fast web surfing; no p2p downloading)
- Username: vpnbook
- Password: bRudre3u
Free OpenVPN (Recommended)
$0/moOpenVPN is the best and most recommended open-source VPN software world-wide. It is the most secure VPN option. You need to download the open-source OpenVPN Client and our configuration and certificate bundle from the links below (use TCP if you cannot connect to UDP due to network restriction).
- Euro1 OpenVPN Certificate Bundle
- Euro2 OpenVPN Certificate Bundle
- UK OpenVPN Certificate Bundle (optimized for fast web surfing; no p2p downloading)
- US OpenVPN Certificate Bundle (optimized for fast web surfing; no p2p downloading)
- All bundles include UDP53, UDP 25000, TCP 80, TCP 443 profile
- Username: vpnbook
- Password: bRudre3u
也就是说,它匹配了过多的内容,我需要的是以第一个</ul>结束前的内容,也就是第一个ul标签内的所有内容。
那么,正则中有这样的匹配方式么?特此我请教了我们的大神马哥,大神曰:看文档去。
我在文档中找到了 贪婪与懒惰 这种匹配模式。
修改表达式为
m = re.search('
- .*?',html,re.S)
匹配到的数据
- euro195.vpnbook.com
- euro213.vpnbook.com
- uk180.vpnbook.com (UK VPN - optimized for fast web surfing; no p2p downloading)
- us1.vpnbook.com (US VPN - optimized for fast web surfing; no p2p downloading)
- Username: vpnbook
- Password: bRudre3u
仅仅多了一个问号,却是天差地别啊。
下面是 贪婪与懒惰 匹配模式的相关知识。
===================================================================================================
当正则表达式中包含能接受重复的限定符时,通常的行为是(在使整个表达式能得到匹配的前提下)匹配尽可能多的字符。以这个表达式为例:a.*b,它将会匹配最长的以a开始,以b结束的字符串。如果用它来搜索aabab的话,它会匹配整个字符串aabab。这被称为贪婪匹配。
有时,我们更需要懒惰匹配,也就是匹配尽可能少的字符。前面给出的限定符都可以被转化为懒惰匹配模式,只要在它后面加上一个问号?。这样.*?就意味着匹配任意数量的重复,但是在能使整个匹配成功的前提下使用最少的重复。现在看看懒惰版的例子吧:
a.*?b匹配最短的,以a开始,以b结束的字符串。如果把它应用于aabab的话,它会匹配aab(第一到第三个字符)和ab(第四到第五个字符)。
代码/语法 | 说明 |
---|---|
*? | 重复任意次,但尽可能少重复 |
+? | 重复1次或更多次,但尽可能少重复 |
?? | 重复0次或1次,但尽可能少重复 |
{n,m}? | 重复n到m次,但尽可能少重复 |
{n,}? | 重复n次以上,但尽可能少重复 |