脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python re正则匹配网页中图片url地址的方法

python re正则匹配网页中图片url地址的方法

2021-05-04 15:04Arckal Python

今天小编就为大家分享一篇python re正则匹配网页中图片url地址的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

最近写了个ref="/article/101143.html">python抓取必应搜索首页http://cn.bing.com/的背景图片并将此图片更换为我的电脑桌面的程序,在正则匹配图片url时遇到了匹配失败问题。

要抓取的图片地址如图所示:

python re正则匹配网页中图片url地址的方法

首先,使用这个pattern

?
1
reg = re.compile('.*g_img={url: "(http.*?jpg)"')

无论怎么匹配都匹配不到,后来把网页源码抓下来放在notepad++中查看,并用notepad++的正则匹配查找,很轻易就匹配到了,如图:

python re正则匹配网页中图片url地址的方法

后来我写了个测试代码,把图片地址在的那一行保存在一个字符串中,很快就匹配到了,如下面代码所示,data是匹配不到的,然而line是可以匹配到的。

?
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
# -*-coding:utf-8-*-
import os
import re
 
f = open('bing.html','r')
 
line = r'''bnp.internal.close(0,0,60056); } });;g_img={url: "https://az12410.vo.msecnd.net/homepage/app/2016hw/binghalloween_bkgimg.jpg",id:'bgdiv',d:'200',cn'''
data = f.read().decode('utf-8','ignore').encode('gbk','ignore')
 
print " "
 
reg = re.compile('.*g_img={url: "(http.*?jpg)"')
 
if re.match(reg, data):
  m1 = reg.findall(data)
  print m1[0]
else:
  print("data not match .")
  
print 20*'-'
#print line
if re.match(reg, line):
  m2 = reg.findall(line)
  print m2[0]
else:
  print("line not match .")

由此可见line和data是有区别的,什么区别呢?那就是data是多行的,包含换行符,而line是单行的,没有换行符。我有在字符串line中加了换行符,结果line没有匹配到。

到这了原因就清楚了。原因就在这句话

?
1
re.compile('.*g_img={url: "(http.*?jpg)"')。

后来翻阅python文档,发现re.compile()这个函数的第二个可选参数flags。这个参数是re中定义的常量,有如下常量

?
1
2
3
re.debug display debug information about compiled expression.
re.i
re.ignorecase perform case-insensitive matching; expressions like [a-z] will match lowercase letters, too. this is not affected by the current locale.
?
1
2
3
4
re.l
 
 
re.locale make \w, \w, \b, \b, \s and \s dependent on the current locale.
?
1
2
3
4
re.m
 
 
re.multiline when specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline). by default, '^' matches only at the beginning of the string, and '$' only at the end of the string and immediately before the newline (if any) at the end of the string.
?
1
2
3
4
re.s
 
 
re.dotall make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline.re.u re.unicode make \w, \w, \b, \b, \d, \d, \s and \s dependent on the unicode character properties database.new in version 2.0.
?
1
2
3
4
re.x
 
 
re.verbose this flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. whitespace within the pattern is ignored, except when in a character class or when preceded by an unescaped backslash. when a line contains a # that is not in a character class and is not preceded by an unescaped backslash, all characters from the leftmost such # through the end of the line are ignored.

这里我们需要的就是re.s 让'.'匹配所有字符,包括换行符。修改正则表达式为

?
1
reg = re.compile('.*g_img={url: "(http.*?jpg)"', re.s)

即可完美解决问题。

以上这篇python re正则匹配网页中图片url地址的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u014108439/article/details/52982461

延伸 · 阅读

精彩推荐