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中Django 后台自定义表单控件
Mar 28 Python
python爬虫爬取快手视频多线程下载功能
Feb 28 Python
Python图像处理之颜色的定义与使用分析
Jan 03 Python
python 用for循环实现1~n求和的实例
Feb 01 Python
wxPython实现画图板
Aug 27 Python
python实现的批量分析xml标签中各个类别个数功能示例
Dec 30 Python
Python selenium页面加载慢超时的解决方案
Mar 18 Python
Django ModelForm操作及验证方式
Mar 30 Python
Python使用plt.boxplot() 参数绘制箱线图
Jun 04 Python
Python生成pdf目录书签的实例方法
Oct 29 Python
python实现企业微信定时发送文本消息的实例代码
Nov 25 Python
用ldap作为django后端用户登录验证的实现
Dec 07 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
模板引擎Smarty深入浅出介绍
2006/12/06 PHP
FirePHP 推荐一款PHP调试工具
2011/04/23 PHP
PHP中SESSION使用中的一点经验总结
2012/03/30 PHP
php addslashes 利用递归实现使用反斜线引用字符串
2013/08/05 PHP
测试PHP连接MYSQL成功与否的代码
2013/08/16 PHP
php防止sql注入之过滤分页参数实例
2014/11/03 PHP
PHP实现防盗链的方法分析
2017/07/25 PHP
JavaScript多线程的实现方法
2007/05/08 Javascript
dropdownlist之间的互相联动实现(显示与隐藏)
2009/11/24 Javascript
css+js实现部分区域高亮可编辑遮罩层
2014/03/04 Javascript
js实现拖拽效果
2015/02/12 Javascript
javascript正则表达式之分组概念与用法实例
2016/06/16 Javascript
jQuery实现的简单在线计算器功能
2017/05/11 jQuery
解决vue打包之后静态资源图片失效的问题
2018/02/21 Javascript
Angular5给组件本身的标签添加样式class的方法
2018/04/07 Javascript
vue里面使用mui的弹出日期选择插件实例
2018/09/16 Javascript
Vue toFixed保留两位小数的3种方式
2020/10/23 Javascript
python解析发往本机的数据包示例 (解析数据包)
2014/01/16 Python
Python socket C/S结构的聊天室应用实现
2014/11/30 Python
Python EOL while scanning string literal问题解决方法
2020/09/18 Python
在Python的Django框架中加载模版的方法
2015/07/16 Python
深入浅析Python中join 和 split详解(推荐)
2016/06/30 Python
Python编程pygal绘图实例之XY线
2017/12/09 Python
python 把列表转化为字符串的方法
2018/10/23 Python
基于PyQt4和PySide实现输入对话框效果
2019/02/27 Python
Python 如何优雅的将数字转化为时间格式的方法
2019/09/26 Python
Python英文文章词频统计(14份剑桥真题词频统计)
2019/10/13 Python
Numpy与Pytorch 矩阵操作方式
2019/12/27 Python
python 画图 图例自由定义方式
2020/04/17 Python
python 字符串的驻留机制及优缺点
2020/06/19 Python
python通过cython加密代码
2020/12/11 Python
ESDlife健康生活易:身体检查预订、搜寻及比较
2019/05/10 全球购物
巴西补充剂和维生素购物网站:Natue
2019/06/17 全球购物
学习新党章思想汇报
2014/01/09 职场文书
求职简历自我评价2015
2015/03/10 职场文书
妈妈再爱我一次观后感
2015/06/08 职场文书