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基于回溯法子集树模板解决旅行商问题(TSP)实例
Sep 05 Python
Python实现抢购IPhone手机
Feb 07 Python
python数据封装json格式数据
Mar 04 Python
python3.5绘制随机漫步图
Aug 27 Python
详解Python 定时框架 Apscheduler原理及安装过程
Jun 14 Python
python输出电脑上所有的串口名的方法
Jul 02 Python
Python zip函数打包元素实例解析
Dec 11 Python
Python参数传递实现过程及原理详解
May 14 Python
详解python 内存优化
Aug 17 Python
python mongo 向数据中的数组类型新增数据操作
Dec 05 Python
详解Python遍历列表时删除元素的正确做法
Jan 07 Python
python中re模块知识点总结
Jan 17 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
萌王史莱姆”萌王性别尴尬!那“萌战”归女组还是男?
2018/12/17 日漫
yii中widget的用法
2014/12/03 PHP
phpQuery让php处理html代码像jQuery一样方便
2015/01/06 PHP
一张表搞清楚php is_null、empty、isset的区别
2015/07/07 PHP
常用Extjs工具:Extjs.util.Format使用方法
2012/03/22 Javascript
jquery实现不包含当前项的选择器实例
2015/06/25 Javascript
jquery实现鼠标滑过后动态图片提示效果实例
2015/08/10 Javascript
基于javascript实现精确到毫秒的倒计时限时抢购
2016/04/17 Javascript
AngularJS表格详解及示例代码
2016/08/17 Javascript
codeMirror插件使用讲解
2017/01/16 Javascript
three.js快速入门【推荐】
2017/01/21 Javascript
前端主流框架vue学习笔记第二篇
2017/07/26 Javascript
Windows下Node.js安装及环境配置方法
2017/09/18 Javascript
基于JavaScript实现表格滚动分页
2017/11/22 Javascript
JS实现运动缓冲效果的封装函数示例
2018/02/18 Javascript
解决在Bootstrap模糊框中使用WebUploader的问题
2018/03/22 Javascript
VUE 配置vue-devtools调试工具及安装方法
2018/09/30 Javascript
vue指令做滚动加载和监听等
2019/05/26 Javascript
angular inputNumber指令输入框只能输入数字的实现
2019/12/03 Javascript
JavaScript中this函数使用实例解析
2020/02/21 Javascript
js中位数不足自动补位扩展padLeft、padRight实现代码
2020/04/06 Javascript
JS实现随机点名器
2020/04/12 Javascript
PyQt5下拉式复选框QComboCheckBox的实例
2019/06/25 Python
python使用openCV遍历文件夹里所有视频文件并保存成图片
2020/01/14 Python
银行实习生自我鉴定范文
2013/09/19 职场文书
新闻记者个人求职的自我评价
2013/11/28 职场文书
教师个人鉴定材料
2014/02/08 职场文书
煤矿安全承诺书
2014/05/22 职场文书
高中军训的心得体会
2014/09/01 职场文书
教师党员学习群众路线心得体会
2014/11/04 职场文书
2014年维修工作总结
2014/11/22 职场文书
教师师德表现自我评价
2015/03/05 职场文书
《夹竹桃》教学反思
2016/02/23 职场文书
写作技巧:如何撰写一份优秀的营销策划书
2019/08/13 职场文书
如何使用SQL Server语句创建表
2022/04/12 SQL Server
MySQL数据库实验实现简单数据库应用系统设计
2022/06/21 MySQL