最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python re正則匹配網(wǎng)頁中圖片url地址的方法

 更新時間:2018年12月20日 09:28:26   作者:Arckal  
今天小編就為大家分享一篇python re正則匹配網(wǎng)頁中圖片url地址的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

最近寫了個python抓取必應(yīng)搜索首頁http://cn.bing.com/的背景圖片并將此圖片更換為我的電腦桌面的程序,在正則匹配圖片url時遇到了匹配失敗問題。

要抓取的圖片地址如圖所示:

python re正則匹配網(wǎng)頁中圖片url地址

首先,使用這個pattern

reg = re.compile('.*g_img={url: "(http.*?jpg)"')

無論怎么匹配都匹配不到,后來把網(wǎng)頁源碼抓下來放在notepad++中查看,并用notepad++的正則匹配查找,很輕易就匹配到了,如圖:

python re正則匹配網(wǎng)頁中圖片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是有區(qū)別的,什么區(qū)別呢?那就是data是多行的,包含換行符,而line是單行的,沒有換行符。我有在字符串line中加了換行符,結(jié)果line沒有匹配到。

到這了原因就清楚了。原因就在這句話

re.compile('.*g_img={url: "(http.*?jpg)"')。

后來翻閱python文檔,發(fā)現(xiàn)re.compile()這個函數(shù)的第二個可選參數(shù)flags。這個參數(shù)是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 讓'.'匹配所有字符,包括換行符。修改正則表達(dá)式為

reg = re.compile('.*g_img={url: "(http.*?jpg)"', re.S)

即可完美解決問題。

以上這篇python re正則匹配網(wǎng)頁中圖片url地址的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python線程的兩種編程方式

    Python線程的兩種編程方式

    這篇文章主要介紹了Python線程的兩種編程方式,Python中如果要使用線程的話,一種是函數(shù)式,一種是用類來包裝的線程對象,需要的朋友可以參考下
    2015-04-04
  • Pytorch中關(guān)于inplace的操作

    Pytorch中關(guān)于inplace的操作

    這篇文章主要介紹了Pytorch中關(guān)于inplace的操作方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 使用Pandas對列名和索引進(jìn)行重命名的幾種常見方法

    使用Pandas對列名和索引進(jìn)行重命名的幾種常見方法

    在數(shù)據(jù)分析和處理中,Pandas是一個非常強(qiáng)大的工具,它提供了靈活的數(shù)據(jù)結(jié)構(gòu)和豐富的操作方法,使得數(shù)據(jù)處理變得更加簡單高效,其中,對數(shù)據(jù)的列名和索引進(jìn)行重命名是常見的需求之一,本文將從基礎(chǔ)概念出發(fā),逐步深入探討如何使用Pandas對列名和索引進(jìn)行重命名
    2024-12-12
  • pymssql ntext字段調(diào)用問題解決方法

    pymssql ntext字段調(diào)用問題解決方法

    pymssql是python用來連接mssql數(shù)據(jù)庫的一個類庫。該庫遵守Python DB API 2.0 標(biāo)準(zhǔn),并且還附帶了一個原生的低階數(shù)據(jù)訪問模塊。
    2008-12-12
  • Windows下Eclipse+PyDev配置Python+PyQt4開發(fā)環(huán)境

    Windows下Eclipse+PyDev配置Python+PyQt4開發(fā)環(huán)境

    這篇文章主要介紹了Windows下Eclipse+PyDev配置Python+PyQt4開發(fā)環(huán)境的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • Python時間戳與日期格式之間相互轉(zhuǎn)化的詳細(xì)教程

    Python時間戳與日期格式之間相互轉(zhuǎn)化的詳細(xì)教程

    java默認(rèn)精度是毫秒級別的,生成的時間戳是13位,而python默認(rèn)是10位的,精度是秒,下面這篇文章主要給大家介紹了關(guān)于Python時間戳與日期格式之間相互轉(zhuǎn)化的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • Python入門(六)Python數(shù)據(jù)類型

    Python入門(六)Python數(shù)據(jù)類型

    這篇文章主要介紹了Python入門(六)Python數(shù)據(jù)類型,Python是一門非常強(qiáng)大好用的語言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下
    2023-04-04
  • Python的組合模式與責(zé)任鏈模式編程示例

    Python的組合模式與責(zé)任鏈模式編程示例

    這篇文章主要介紹了Python的組合模式與責(zé)任鏈模式編程示例,組合模式與責(zé)任鏈模式都屬于Python的設(shè)計模式,需要的朋友可以參考下
    2016-02-02
  • PyTorch如何創(chuàng)建自己的數(shù)據(jù)集

    PyTorch如何創(chuàng)建自己的數(shù)據(jù)集

    這篇文章主要介紹了PyTorch如何創(chuàng)建自己的數(shù)據(jù)集,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • python編碼格式導(dǎo)致csv讀取錯誤問題(csv.reader, pandas.csv_read)

    python編碼格式導(dǎo)致csv讀取錯誤問題(csv.reader, pandas.csv_read)

    python編碼格式導(dǎo)致csv讀取錯誤問題(csv.reader, pandas.csv_read),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05

最新評論

县级市| 湘潭县| 那曲县| 米脂县| 泰和县| 老河口市| 怀集县| 博乐市| 西吉县| 雅安市| 舞钢市| 阿拉善左旗| 贡山| 镇远县| 甘洛县| 杭州市| 金塔县| 金平| 崇阳县| 滦南县| 灵宝市| 沙雅县| 罗甸县| 从化市| 隆德县| 廊坊市| 白河县| 通道| 鸡西市| 长垣县| 镇沅| 万盛区| 轮台县| 涿鹿县| 清丰县| 申扎县| 哈密市| 宕昌县| 娄烦县| 垦利县| 杂多县|