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 相关文章推荐
python3生成随机数实例
Oct 20 Python
python使用webbrowser浏览指定url的方法
Apr 04 Python
Python中计算三角函数之cos()方法的使用简介
May 15 Python
Python实现爬取亚马逊数据并打印出Excel文件操作示例
May 16 Python
python监控进程状态,记录重启时间及进程号的实例
Jul 15 Python
python列表推导式入门学习解析
Dec 02 Python
python操作cfg配置文件方式
Dec 22 Python
TensorFlow命名空间和TensorBoard图节点实例
Jan 23 Python
python GUI库图形界面开发之PyQt5菜单栏控件QMenuBar的详细使用方法与实例
Feb 28 Python
python爬虫开发之Beautiful Soup模块从安装到详细使用方法与实例
Mar 09 Python
django 数据库 get_or_create函数返回值是tuple的问题
May 15 Python
使用PyQt的QLabel组件实现选定目标框功能的方法示例
May 19 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 curl基本操作详解
2013/07/23 PHP
PHP 根据key 给二维数组分组
2016/12/09 PHP
PHP创建XML接口示例
2019/07/04 PHP
PHP7 list() 函数修改
2021/03/09 PHP
一个页面放2段图片滚动代码出现冲突的问题如何解决
2012/12/21 Javascript
JavaScript 上万关键字瞬间匹配实现代码
2013/07/07 Javascript
JavaScript 七大技巧(一)
2015/12/13 Javascript
vue.js 表格分页ajax 异步加载数据
2016/10/18 Javascript
微信小程序 轮播图swiper详解及实例(源码下载)
2017/01/11 Javascript
微信小程序学习(4)-系统配置app.json详解
2017/01/12 Javascript
JavaScript上传文件时不用刷新页面方法总结(推荐)
2017/08/15 Javascript
详解基于mpvue的小程序markdown适配解决方案
2018/05/08 Javascript
对mac下nodejs 更新到最新版本的最新方法(推荐)
2018/05/17 NodeJs
Webpack中loader打包各种文件的方法实例
2019/09/03 Javascript
linux系统使用python获取cpu信息脚本分享
2014/01/15 Python
python实现基本进制转换的方法
2015/07/11 Python
使用python遍历指定城市的一周气温
2017/03/31 Python
python 简单备份文件脚本v1.0的实例
2017/11/06 Python
Python2.7实现多进程下开发多线程示例
2019/05/31 Python
python实现对图片进行旋转,放缩,裁剪的功能
2019/08/07 Python
Python绘制热力图示例
2019/09/27 Python
.img/.hdr格式转.nii格式的操作
2020/07/01 Python
Python基于execjs运行js过程解析
2020/11/27 Python
详解CSS3的box-shadow属性制作边框阴影效果的方法
2016/05/10 HTML / CSS
linux面试题参考答案(9)
2015/01/07 面试题
财务会计应届生求职信
2013/11/24 职场文书
幼儿园家长寄语
2014/04/02 职场文书
财务管理专业求职信
2014/06/11 职场文书
个人融资协议书
2014/10/02 职场文书
护士个人年度总结范文
2015/02/13 职场文书
南京南京观后感
2015/06/02 职场文书
五星红旗迎风飘扬观后感
2015/06/17 职场文书
八年级作文之我的母亲
2019/12/10 职场文书
MySQL为id选择合适的数据类型
2021/06/07 MySQL
实操Python爬取觅知网素材图片示例
2021/11/27 Python
Redis基本数据类型String常用操作命令
2022/06/01 Redis