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


Posted in Python onDecember 20, 2018

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

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

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

首先,使用这个pattern

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

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

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

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

# -*-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没有匹配到。

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

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

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

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.
re.L 


re.LOCALE Make \w, \W, \b, \B, \s and \S dependent on the current locale.
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.
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.
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 让'.'匹配所有字符,包括换行符。修改正则表达式为

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

即可完美解决问题。

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

Python 相关文章推荐
python调用cmd复制文件代码分享
Dec 27 Python
Python3实现将文件树中所有文件和子目录归档到tar压缩文件的方法
May 22 Python
对于Python装饰器使用的一些建议
Jun 03 Python
编写Python小程序来统计测试脚本的关键字
Mar 12 Python
python去除文件中空格、Tab及回车的方法
Apr 12 Python
Python基础练习之用户登录实现代码分享
Nov 08 Python
浅谈python数据类型及类型转换
Dec 18 Python
Python基于whois模块简单识别网站域名及所有者的方法
Apr 23 Python
解决Python requests库编码 socks5代理的问题
May 07 Python
python实现局域网内实时通信代码
Dec 22 Python
谈一谈数组拼接tf.concat()和np.concatenate()的区别
Feb 07 Python
Elasticsearch 基本查询和组合查询
Apr 19 Python
python使用pdfminer解析pdf文件的方法示例
Dec 20 #Python
python爬取指定微信公众号文章
Dec 20 #Python
在Django中URL正则表达式匹配的方法
Dec 20 #Python
python采集微信公众号文章
Dec 20 #Python
Linux下Pycharm、Anaconda环境配置及使用踩坑
Dec 19 #Python
python爬虫之urllib,伪装,超时设置,异常处理的方法
Dec 19 #Python
python3实现网络爬虫之BeautifulSoup使用详解
Dec 19 #Python
You might like
PHP的FTP学习(一)[转自奥索]
2006/10/09 PHP
深入理解PHP之OpCode原理详解
2016/06/01 PHP
PHP实现文件上传下载实例
2016/10/18 PHP
php读取本地json文件的实例
2018/03/07 PHP
js tab 选项卡
2009/04/26 Javascript
ExtJS[Desktop]实现图标换行示例代码
2013/11/17 Javascript
关于Javascript 对象(object)的prototype
2014/05/09 Javascript
PHP 数组current和next用法分享
2015/03/05 Javascript
jQuery实现两款有动画功能的导航菜单代码
2015/09/16 Javascript
使用Node.js处理前端代码文件的编码问题
2016/02/16 Javascript
AngularJS出现$http异步后台无法获取请求参数问题的解决方法
2016/11/03 Javascript
微信JSAPI支付操作需要注意的细节
2017/01/10 Javascript
JS中去掉array中重复元素的方法
2017/05/26 Javascript
JavaScript中防止微信浏览器被整体拖动的方法
2017/08/25 Javascript
vue-cli初始化项目中使用less的方法
2018/08/09 Javascript
浅谈Vue render函数在ElementUi中的应用
2018/09/06 Javascript
bootstrap table合并行数据并居中对齐效果
2018/10/17 Javascript
js删除数组中某几项的方法总结
2019/01/16 Javascript
JavaScript中的this妙用实例分析
2020/05/09 Javascript
Vue Element校验validate的实例
2020/09/21 Javascript
python选择排序算法的实现代码
2013/11/21 Python
利用Python批量生成任意尺寸的图片
2016/08/29 Python
Python实现钉钉发送报警消息的方法
2019/02/20 Python
500行Python代码打造刷脸考勤系统
2019/06/03 Python
树莓派+摄像头实现对移动物体的检测
2019/06/22 Python
Series和DataFrame使用简单入门
2019/11/13 Python
10种CSS3实现的loading动画,挑一个走吧?
2020/11/16 HTML / CSS
突袭HTML5之Javascript API扩展2—地理信息服务及地理位置API学习
2013/01/31 HTML / CSS
String和StringBuffer的区别
2015/08/13 面试题
入党积极分子自我鉴定范文
2014/03/25 职场文书
汽车维修专业自荐书
2014/05/26 职场文书
篮球赛口号
2014/06/18 职场文书
表扬通报怎么写
2015/01/16 职场文书
2015年计生工作总结范文
2015/04/24 职场文书
居住证明范文
2015/06/17 职场文书
Canvas跟随鼠标炫彩小球的实现
2021/04/11 Javascript