99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

python中re正則匹配網頁中圖片url地址的示例分析-創新互聯

這篇文章主要為大家展示了“python中re正則匹配網頁中圖片url地址的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“python中re正則匹配網頁中圖片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地址的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯成都網站設計公司行業資訊頻道!

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

新聞標題:python中re正則匹配網頁中圖片url地址的示例分析-創新互聯
標題網址:http://www.yijiale78.com/article40/dpjheo.html

成都網站建設公司_創新互聯,為您提供商城網站、網站內鏈、網站設計公司、移動網站建設、網站制作、全網營銷推廣

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

外貿網站制作