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 (2)
Oct 31 Python
使用python绘制人人网好友关系图示例
Apr 01 Python
跟老齐学Python之for循环语句
Oct 02 Python
Python3 能振兴 Python的原因分析
Nov 28 Python
深入解析Python中的lambda表达式的用法
Aug 28 Python
Python实现压缩与解压gzip大文件的方法
Sep 18 Python
python中多层嵌套列表的拆分方法
Jul 02 Python
python实现根据指定字符截取对应的行的内容方法
Oct 23 Python
基于Django静态资源部署404的解决方法
Jul 28 Python
django 解决model中类写不到数据库中,数据库无此字段的问题
May 20 Python
对象析构函数__del__在Python中何时使用
Mar 22 Python
python神经网络学习 使用Keras进行回归运算
May 04 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 短链接算法收集与分析
2011/12/30 PHP
用PHP实现 上一篇、下一篇的代码
2012/09/29 PHP
thinkphp控制器调度使用示例
2014/02/24 PHP
ThinkPHP基本的增删查改操作实例教程
2014/08/22 PHP
PHP获取二叉树镜像的方法
2018/01/17 PHP
jquery 表单进行客户端验证demo
2009/08/24 Javascript
经典海量jQuery插件 大家可以收藏一下
2010/02/07 Javascript
JS数学函数Exp使用说明
2012/08/09 Javascript
kindeditor修复会替换script内容的问题
2015/04/03 Javascript
基于PHP和Mysql相结合使用jqGrid读取数据并显示
2015/12/02 Javascript
js控制台输出的方法(详解)
2016/11/26 Javascript
前端编码规范(3)JavaScript 开发规范
2017/01/21 Javascript
Vue响应式添加、修改数组和对象的值
2017/03/20 Javascript
Layui数据表格 前后端json数据接收的方法
2019/09/19 Javascript
js中关于Blob对象的介绍与使用
2019/11/29 Javascript
JavaScript实现联动菜单特效
2020/01/07 Javascript
公众号SVG动画交互实战代码
2020/05/31 Javascript
js实现带积分弹球小游戏
2020/07/21 Javascript
[00:12]DAC2018 天才少年转战三号位,他的SOLO是否仍如昔日般强大?
2018/04/06 DOTA
python中的变量如何开辟内存
2018/06/26 Python
python 使用opencv 把视频分割成图片示例
2019/12/12 Python
Python原始套接字编程实例解析
2020/01/29 Python
对tensorflow中tf.nn.conv1d和layers.conv1d的区别详解
2020/02/11 Python
Django配置跨域并开发测试接口
2020/11/04 Python
详解如何在css中引入自定义字体(font-face)
2018/05/17 HTML / CSS
如何用border-image实现文字气泡边框的示例代码
2020/01/21 HTML / CSS
菲律宾领先的在线时尚商店:Zalora菲律宾
2018/02/08 全球购物
俄罗斯最大的隐形眼镜销售网站:Ochkov.Net
2021/02/07 全球购物
本科生详细的自我评价
2013/09/19 职场文书
《棉鞋里的阳光》教学反思
2014/04/24 职场文书
红色旅游心得体会
2014/09/03 职场文书
2016庆祝国庆67周年宣传语
2015/11/25 职场文书
2016优秀大学生个人事迹材料范文
2016/03/01 职场文书
vue实现简单数据双向绑定
2021/04/28 Vue.js
磁贴还没死, 微软Win11可修改注册表找回Win10开始菜单
2021/11/21 数码科技