欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

首先,使用這個(gè)pattern

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

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

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

后來我寫了個(gè)測(cè)試代碼,把圖片地址在的那一行保存在一個(gè)字符串中,很快就匹配到了,如下面代碼所示,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()這個(gè)函數(shù)的第二個(gè)可選參數(shù)flags。這個(gè)參數(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)頁(yè)中圖片url地址的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Django自定義權(quán)限及用戶分組

    Django自定義權(quán)限及用戶分組

    這篇文章主要為大家介紹了Django登錄權(quán)限及分組模板使用權(quán)限,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Python實(shí)現(xiàn)數(shù)據(jù)的序列化操作詳解

    Python實(shí)現(xiàn)數(shù)據(jù)的序列化操作詳解

    在日常開發(fā)中,對(duì)數(shù)據(jù)進(jìn)行序列化和反序列化是常見的數(shù)據(jù)操作,Python提供了兩個(gè)模塊方便開發(fā)者實(shí)現(xiàn)數(shù)據(jù)的序列化操作,即?json?模塊和?pickle?模塊。本文就為大家詳細(xì)講解這兩個(gè)模塊的使用,需要的可以參考一下
    2022-07-07
  • tensorflow之如何使用GPU而不是CPU問題

    tensorflow之如何使用GPU而不是CPU問題

    這篇文章主要介紹了tensorflow之如何使用GPU而不是CPU問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Python實(shí)現(xiàn)判斷給定列表是否有重復(fù)元素的方法

    Python實(shí)現(xiàn)判斷給定列表是否有重復(fù)元素的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)判斷給定列表是否有重復(fù)元素的方法,列舉了2種列表判斷方法,涉及Python針對(duì)列表的遍歷、統(tǒng)計(jì)、判斷等相關(guān)操作技巧,需要的朋友可以參考下
    2018-04-04
  • 利用Python實(shí)現(xiàn)在PDF文檔中插入文字水印

    利用Python實(shí)現(xiàn)在PDF文檔中插入文字水印

    在傳播PDF文檔的過程中,如何有效地保護(hù)文檔的版權(quán)和所有權(quán),防止非法復(fù)制和濫用,成為了一個(gè)不可忽視的問題,所以給PDF文檔添加水印便成了一種行之有效的保護(hù)手,本文將展示如何使用Python在PDF文檔中插入文字水印,實(shí)現(xiàn)高效的PDF文檔處理,需要的朋友可以參考下
    2024-04-04
  • 解決python 3 urllib 沒有 urlencode 屬性的問題

    解決python 3 urllib 沒有 urlencode 屬性的問題

    今天小編就為大家分享一篇解決python 3 urllib 沒有 urlencode 屬性的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python淘寶秒殺的腳本實(shí)現(xiàn)

    Python淘寶秒殺的腳本實(shí)現(xiàn)

    這篇文章主要介紹了Python淘寶秒殺的腳本實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Python中使用Opencv開發(fā)停車位計(jì)數(shù)器功能

    Python中使用Opencv開發(fā)停車位計(jì)數(shù)器功能

    這篇文章主要介紹了Python中使用Opencv開發(fā)停車位計(jì)數(shù)器,本教程最好的一點(diǎn)就是我們將使用基本的圖像處理技術(shù)來解決這個(gè)問題,沒有使用機(jī)器學(xué)習(xí)、深度學(xué)習(xí)進(jìn)行訓(xùn)練來識(shí)別,感興趣的朋友跟隨小編一起看看吧
    2022-04-04
  • Python中實(shí)現(xiàn)變量賦值傳遞時(shí)的引用和拷貝方法

    Python中實(shí)現(xiàn)變量賦值傳遞時(shí)的引用和拷貝方法

    下面小編就為大家分享一篇Python中實(shí)現(xiàn)變量賦值傳遞時(shí)的引用和拷貝方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python實(shí)現(xiàn)時(shí)鐘顯示效果思路詳解

    Python實(shí)現(xiàn)時(shí)鐘顯示效果思路詳解

    這篇文章主要介紹了Python實(shí)現(xiàn)時(shí)鐘顯示,需要的朋友可以參考下
    2018-04-04

最新評(píng)論