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天气预报采集器实现代码(网页爬虫)
Oct 07 Python
Python判断操作系统类型代码分享
Nov 22 Python
django 按时间范围查询数据库实例代码
Feb 11 Python
对python字典元素的添加与修改方法详解
Jul 06 Python
python实现linux下抓包并存库功能
Jul 18 Python
python实现括号匹配的思路详解
Aug 23 Python
删除DataFrame中值全为NaN或者包含有NaN的列或行方法
Nov 06 Python
python使用Plotly绘图工具绘制柱状图
Apr 01 Python
opencv3/python 鼠标响应操作详解
Dec 11 Python
Python注释、分支结构、循环结构、伪“选择结构”用法实例分析
Jan 09 Python
Pytest如何使用skip跳过执行测试
Aug 13 Python
20行代码教你用python给证件照换底色的方法示例
Feb 05 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入门
2006/10/09 PHP
php下利用curl判断远程文件是否存在的实现代码
2011/10/08 PHP
PHP中3种生成XML文件方法的速度效率比较
2012/10/06 PHP
PHP的switch判断语句的“高级”用法详解
2014/10/01 PHP
php无限极分类实现方法分析
2019/07/04 PHP
克隆javascript对象的三个方法小结
2011/01/12 Javascript
轻松实现javascript图片轮播特效
2016/01/13 Javascript
基于canvas实现的绚丽圆圈效果完整实例
2016/01/26 Javascript
JavaScript中三种异步上传文件方式
2016/03/06 Javascript
js实现简单的计算器功能
2017/01/16 Javascript
bootstrap table 数据表格行内修改的实现代码
2017/02/13 Javascript
js的OOP继承实现(必看篇)
2017/02/18 Javascript
angularjs实现table增加tr的方法
2018/02/27 Javascript
深入浅析JS中的严格模式
2018/06/04 Javascript
详解Eslint 配置及规则说明
2018/09/10 Javascript
node.js基于socket.io快速实现一个实时通讯应用
2019/04/23 Javascript
小程序多图列表实现性能优化的方法步骤
2019/05/28 Javascript
vue 实现通过vuex 存储值 在不同界面使用
2019/11/11 Javascript
JS如何在数组指定位置插入元素
2020/03/10 Javascript
vue 封装面包屑组件教程
2020/11/16 Javascript
[03:55]显微镜下的DOTA2特别篇——430灰烬之灵神级操作
2014/06/24 DOTA
[53:15]Mineski vs iG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python基于urllib实现按照百度音乐分类下载mp3的方法
2015/05/25 Python
浅谈Python 对象内存占用
2016/07/15 Python
解析Python3中的Import
2019/10/13 Python
解决tensorflow添加ptb库的问题
2020/02/10 Python
Pandas对DataFrame单列/多列进行运算(map, apply, transform, agg)
2020/06/14 Python
sqlalchemy实现时间列自动更新教程
2020/09/02 Python
遮罩层 + Iframe实现界面自动显示的示例代码
2020/04/26 HTML / CSS
英国最大的香水商店:The Fragrance Shop
2018/07/06 全球购物
银行授权委托书范本
2014/10/04 职场文书
2014年教师工作总结
2014/11/10 职场文书
给老婆的检讨书(搞笑版)
2015/05/06 职场文书
家庭聚会祝酒词
2015/08/11 职场文书
详解Redis瘦身指南
2021/05/26 Redis
Python开发之QT解决无边框界面拖动卡屏问题(附带源码)
2021/05/27 Python