2016-07-28 50 views
-3

我是新來的Python,我試圖從網站獲得ALT圖片來源,但我現在面臨的問題與報價'"Python的正則表達式查找圖像源

import requests,urllib,urllib2,re 

rule = re.compile(r'^[^*$<,>?!\']*$') 

r = requests.get('http://www.hotstar.com/channels/star-plus') 
match = re.compile('<img alt="(.*?)" ng-mouseleave="mouseLeaveCard()" ng-mouseenter="mouseEnterCard()" ng-click="mouseEnterCard(true)" ng-class="{\'dull-img\': isThumbnailTitleVisible || isRegionalLanguageVisible}" class="show-card imgtag card-minheight-hc ng-scope ng-isolate-scope" placeholder-img="{\'realUrl\' : \'(.*?)\', \'placeholderUrl\' : \'./img/placeholder/hs.jpg\'}" ng-if="record.urlPictures" src="(.*?)" style="display: block;">',re.DOTALL).findall(r.content) 
for name,img,image in match: 

我只能使用標準的Python庫。

我讀過有關定義規則,所以我從這個做:Regex Apostrophe how to match?

老實說,我不知道如何使用它。

在此先感謝

+0

你的輸入是什麼,你的預期輸出是什麼? – Jokab

回答

0

使用解析器來代替:

import requests 
from bs4 import BeautifulSoup 
r = requests.get('http://www.hotstar.com/channels/star-plus') 
soup = BeautifulSoup(r.text, "lxml") 
imgs = soup.findAll('img') 
for img in imgs: 
    print(img["alt"]) 
+0

這給了我以下輸出:* – Ohumeronen

+0

@Ohumeronen:我知道。它會打印出每個找到的圖像的所有「alt」屬性。 – Jan

+0

@Jan我也得到輸出爲'*'沒有別的 –

0

我剛剛看了一下這個問題,我試圖尋找到,我發現了幾個不同的方式去了解它看下面的鏈接。看起來像這樣的事情發生在其他人身上。我快速瀏覽了一下,並認爲這些可能會有所幫助。嘗試尋找在以下幾個頁面:

可能類似的帖子:

那麼你也可以嘗試尋找Python's Regular Expression Documentation