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格式化css文件的方法
Mar 10 Python
在Linux下调试Python代码的各种方法
Apr 17 Python
Python3下错误AttributeError: ‘dict’ object has no attribute’iteritems‘的分析与解决
Jul 06 Python
python读取和保存图片5种方法对比
Sep 12 Python
Django使用unittest模块进行单元测试过程解析
Aug 02 Python
使用Python实现画一个中国地图
Nov 23 Python
使用pyqt 实现重复打开多个相同界面
Dec 13 Python
pytorch 利用lstm做mnist手写数字识别分类的实例
Jan 10 Python
利用Pytorch实现简单的线性回归算法
Jan 15 Python
使用python实现CGI环境搭建过程解析
Apr 28 Python
谈谈python垃圾回收机制
Sep 27 Python
Python使用cn2an实现中文数字与阿拉伯数字的相互转换
Mar 02 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密码生成类实例
2014/09/24 PHP
php+mysql+jquery实现日历签到功能
2017/02/27 PHP
Laravel学习教程之本地化模块
2017/08/18 PHP
PHP实现求两个字符串最长公共子串的方法示例
2017/11/17 PHP
js 对象是否存在判断
2009/07/15 Javascript
jquery 可排列的表实现代码
2009/11/13 Javascript
javascript正则匹配汉字、数字、字母、下划线
2014/04/10 Javascript
jquery显示隐藏input对象
2014/07/21 Javascript
js通过location.search来获取页面传来的参数
2014/09/11 Javascript
js实现类似jquery里animate动画效果的方法
2015/04/10 Javascript
jQuery绑定事件on()与弹窗的简要概述
2016/04/27 Javascript
vue组件如何被其他项目引用
2017/04/13 Javascript
JavaScript函数中的this四种绑定形式
2017/08/15 Javascript
vue项目使用微信公众号支付总结及遇到的坑
2018/10/23 Javascript
vue-better-scroll 的使用实例代码详解
2018/12/03 Javascript
vue2.0 解决抽取公用js的问题
2020/07/31 Javascript
swiper实现导航滚动效果
2020/12/13 Javascript
vue中配置scss全局变量的步骤
2020/12/28 Vue.js
[01:01]青春无憾,一战成名——DOTA2全国高校联赛开启
2018/02/25 DOTA
[01:02:09]Liquid vs TNC 2019国际邀请赛淘汰赛 胜者组 BO3 第二场 8.21
2020/07/19 DOTA
Python爬虫框架scrapy实现downloader_middleware设置proxy代理功能示例
2018/08/04 Python
python将txt文件读取为字典的示例
2018/12/22 Python
Django网络框架之创建虚拟开发环境操作示例
2019/06/06 Python
Python Django Cookie 简单用法解析
2019/08/13 Python
Python的控制结构之For、While、If循环问题
2020/06/30 Python
当当网官方旗舰店:中国图书销售夺金品牌
2018/04/02 全球购物
英国家电购物网站:Sonic Direct
2019/03/26 全球购物
JSF面试题:如何管量web层中的Bean,用什么标签。如何通过jsp页面与Bean绑定在一起进行处理?
2012/10/05 面试题
P/Invoke是什么
2015/07/31 面试题
品恩科技软件测试面试题
2014/10/26 面试题
写演讲稿所需要注意的4个条件
2014/01/09 职场文书
消防安全责任书
2014/04/14 职场文书
工伤事故证明
2014/10/20 职场文书
2014教师年度思想工作总结
2014/11/10 职场文书
Go语言实现Base64、Base58编码与解码
2021/07/26 Golang
HTML+JS实现在线朗读器
2022/02/15 Javascript