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写入中英文字符串到文件的方法
May 06 Python
python实现爬虫统计学校BBS男女比例(一)
Dec 31 Python
Python基于回溯法子集树模板解决取物搭配问题实例
Sep 02 Python
基于scrapy的redis安装和配置方法
Jun 13 Python
Python实现随机漫步功能
Jul 09 Python
Python+OpenCV图片局部区域像素值处理详解
Jan 23 Python
python接口自动化(十七)--Json 数据处理---一次爬坑记(详解)
Apr 18 Python
python分数表示方式和写法
Jun 26 Python
python程序 线程队列queue使用方法解析
Sep 23 Python
Python使用循环神经网络解决文本分类问题的方法详解
Jan 16 Python
Django model重写save方法及update踩坑详解
Jul 27 Python
基于python实现简单网页服务器代码实例
Sep 14 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
全新Mac配置PHP开发环境教程
2016/02/03 PHP
PHP中时间加减函数strtotime用法分析
2017/04/26 PHP
IOS 开发之NSDictionary转换成JSON字符串
2017/08/14 PHP
PHP实现的pdo连接数据库并插入数据功能简单示例
2019/03/30 PHP
IE 条件注释详解总结(附实例代码)
2009/08/29 Javascript
JQuery 插件制作实践 xMarquee插件V1.0
2010/04/02 Javascript
javascript操作JSON的要领总结
2012/12/09 Javascript
js保留两位小数使用toFixed实现
2013/07/29 Javascript
jquery跟随屏幕滚动效果的实现代码
2016/04/13 Javascript
JS上传图片预览插件制作(兼容到IE6)
2016/08/07 Javascript
jQuery常用样式操作实例分析(获取、设置、追加、删除、判断等)
2016/09/08 Javascript
基于jQuery的select下拉框选择触发事件实例分析
2016/11/18 Javascript
Vuex之理解Getters的用法实例
2017/04/19 Javascript
JS实现简单短信验证码界面
2017/08/07 Javascript
使用RN Animated做一个“添加购物车”动画的方法
2018/09/12 Javascript
移动端自适应flexible.js的使用方法(不用三大框架,仅写一个单html页面使用)推荐
2019/04/02 Javascript
如何让Nodejs支持H5 History模式(connect-history-api-fallback源码分析)
2019/05/30 NodeJs
微信小程序实现页面跳转传递参数(实体,对象)
2019/08/12 Javascript
createObjectURL方法实现本地图片预览
2019/09/30 Javascript
jQuery实现轮播图效果
2019/11/26 jQuery
python删除文件示例分享
2014/01/28 Python
Python 字典与字符串的互转实例
2017/01/13 Python
解决TensorFlow调用Keras库函数存在的问题
2020/07/06 Python
css3 box-shadow阴影(外阴影与外发光)图示讲解
2017/08/11 HTML / CSS
Farnell德国:电子元器件供应商
2018/07/10 全球购物
计算机网络专业个人的自我评价
2013/10/17 职场文书
大学生个人简历自我评价
2013/11/16 职场文书
高中校园广播稿
2014/01/11 职场文书
毕业生大学生活自我总结
2014/01/31 职场文书
绿色出行口号
2014/06/18 职场文书
2014年班组长工作总结
2014/11/20 职场文书
换届选举主持词
2015/07/03 职场文书
爱心捐赠活动简讯
2015/07/20 职场文书
2016年优秀共产党员先进事迹材料
2016/02/29 职场文书
Python办公自动化之教你用Python批量识别发票并录入到Excel表格中
2021/06/26 Python
html,css,javascript是怎样变成页面的
2023/05/07 HTML / CSS