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基础教程之实现石头剪刀布游戏示例
Feb 11 Python
Python写的一个简单监控系统
Jun 19 Python
python装饰器实例大详解
Oct 25 Python
Python面向对象编程基础解析(二)
Oct 26 Python
Python基于PyGraphics包实现图片截取功能的方法
Dec 21 Python
Python读取Json字典写入Excel表格的方法
Jan 03 Python
使用Python+wxpy 找出微信里把你删除的好友实例
Feb 21 Python
python高阶函数map()和reduce()实例解析
Mar 16 Python
解决jupyter notebook打不开无反应 浏览器未启动的问题
Apr 10 Python
Django解决frame拒绝问题的方法
Dec 18 Python
pytorch 中forward 的用法与解释说明
Feb 26 Python
pytorch 6 batch_train 批训练操作
May 28 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
虹吸壶煮咖啡26个注意事项
2021/03/03 冲泡冲煮
PHP addAttribute()函数讲解
2019/02/03 PHP
Jquery post传递数组方法实现思路及代码
2013/04/28 Javascript
点击A元素触发B元素的事件在IE8下会识别成A元素
2014/09/04 Javascript
asp.net+js实现金额格式化
2015/02/27 Javascript
JavaScript中的依赖注入详解
2015/03/18 Javascript
JavaScript模板引擎用法实例
2015/07/10 Javascript
Easyui 之 Treegrid 笔记
2016/04/29 Javascript
JavaScript动态添加事件之事件委托
2016/07/12 Javascript
JavaScript奇技淫巧44招【实用】
2016/12/11 Javascript
Jquery实现跨域异步上传文件总结
2017/02/03 Javascript
javascript中join方法实例讲解
2019/02/21 Javascript
Vue.extend实现挂载到实例上的方法
2019/05/01 Javascript
javascript实现导航栏分页效果
2019/06/27 Javascript
layui弹出框Tab选项卡的示例代码
2019/09/04 Javascript
JS箭头函数和常规函数之间的区别实例分析【 5 个区别】
2020/05/27 Javascript
Python中列表、字典、元组、集合数据结构整理
2014/11/20 Python
python利用urllib和urllib2访问http的GET/POST详解
2017/09/27 Python
python之Flask实现简单登录功能的示例代码
2018/12/24 Python
python的pyecharts绘制各种图表详细(附代码)
2019/11/11 Python
Python turtle库绘制菱形的3种方式小结
2019/11/23 Python
使用python-Jenkins批量创建及修改jobs操作
2020/05/12 Python
KEETSA环保床垫:更好的睡眠,更好的生活!
2016/11/24 全球购物
办公室前台岗位职责
2014/01/04 职场文书
《白鹅》教学反思
2014/04/13 职场文书
小学生保护环境倡议书
2014/05/15 职场文书
一年级班主任工作总结2014
2014/11/08 职场文书
财政局个人总结
2015/03/04 职场文书
2015年文员个人工作总结
2015/04/09 职场文书
爱心捐款活动总结
2015/05/09 职场文书
2015年幼儿园教育教学工作总结
2015/05/25 职场文书
2016年优秀党员教师先进事迹材料
2016/02/29 职场文书
2019年关于小学生课外阅读情况的分析报告
2019/12/02 职场文书
Python 中的单分派泛函数你真的了解吗
2021/06/22 Python
总结Python变量的相关知识
2021/06/28 Python
Windows Server 2022 超融合部署(图文教程)
2022/06/25 Servers