2011-01-29 37 views
1

我有一些网页的源代码,我需要找到标签的所有事件,并提取图片的名称和位置(例如<img src="../images/test.jpg" />我需要 path="../images/"file="test.jpg")。我如何用正则表达式来做到这一点?提取路径和文件名从<img >标签

回答

4

你应该使用lxml.html

>>> from urllib2 import urlopen 
>>> from lxml import html 
>>> page = urlopen('http://www.amazon.co.uk/') 
>>> page_source = html.parse(page) 
>>> from pprint import pprint 
>>> pprint(page_source.xpath('//img/@src')) 
['http://g-ecx.images-amazon.com/images/G/02/gno/images/orangeBlue/navPackedSprites-UK-15._V202471918_.png', 
'http://g-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192199769_.gif', 
'http://g-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192199769_.gif', 
'http://g-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192199769_.gif', 
'http://g-ecx.images-amazon.com/images/G/02/uk-marketing/xmas10/janbargains/uk-january-bargains-loz75._V175451391_.gif', 
'http://g-ecx.images-amazon.com/images/G/02/UK-Shoe/email/7_jan_11-amzn-sale-loz-1._V173375114_.png', 
'http://g-ecx.images-amazon.com/images/G/02/uk-jw/homepage/uk-wtch-police-roto._V185455265_.png', 
'http://g-ecx.images-amazon.com/images/G/02/kindle/shasta/merch/gw/shasta-gw-bestselling-01a-470x265._V173993687_.jpg', 
'http://ecx.images-amazon.com/images/I/412wF8LJ-uL._SL135_.jpg', 
'http://ecx.images-amazon.com/images/I/51YC5H64AuL._SL135_.jpg', 
'http://ecx.images-amazon.com/images/I/41%2BdpTvM1FL._SL135_.jpg', 
'http://z-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192234675_.gif', 
'http://z-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192234675_.gif', 
'http://g-ecx.images-amazon.com/images/G/01/x-locale/common/transparent-pixel._V42752373_.gif', 
'http://z-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192234675_.gif', 
'http://z-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192234675_.gif', 
'http://g-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192199769_.gif', 
'http://ecx.images-amazon.com/images/I/51-kiOR0NwL._SL135_.jpg', 
'http://ecx.images-amazon.com/images/I/51DRc-7HuxL._SL135_.jpg', 
'http://ecx.images-amazon.com/images/I/51SK5htD22L._SL135_.jpg', 
'http://z-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192234675_.gif', 
'http://z-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192234675_.gif', 
'http://z-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192234675_.gif', 
'http://z-ecx.images-amazon.com/images/G/02/x-locale/common/transparent-pixel._V192234675_.gif', 
'http://ecx.images-amazon.com/images/I/31POT%2BzL1tL._SS120_RO10,1,201,225,243,255,255,255,15_.jpg', 
'http://ecx.images-amazon.com/images/I/41hkDkhjrTL._SS120_RO10,1,201,225,243,255,255,255,15_.jpg', 
'http://ecx.images-amazon.com/images/I/41zDYiAWasL._SS120_RO10,1,201,225,243,255,255,255,15_.jpg', 
'http://ecx.images-amazon.com/images/I/31HqB5H8j%2BL._SS120_RO10,1,201,225,243,255,255,255,15_.jpg', 
'http://g-ecx.images-amazon.com/images/G/02/uk-clothing/Lingerie/UK_APP_LingerieStore_50._V171062881_.png', 
'http://g-ecx.images-amazon.com/images/G/02/uk-pets/graphics/B000FVC1HE_50._V198692831_.jpg', 
'http://g-ecx.images-amazon.com/images/G/02/uk-grocery/images/illy_50._V198779066_.gif', 
'http://g-ecx.images-amazon.com/images/G/02/uk-electronics/MI_Store/UK_MIN_MILaunch_50._V191178779_.png', 
'http://g-ecx.images-amazon.com/images/G/02/uk-lighting/graphics/NoveltyLighting_50._V192237013_.jpg', 
'http://g-ecx.images-amazon.com/images/G/02/UK-Shoe/email/7_jan_11-amzn-sale-TCG-1._V173375108_.png', 
'http://g-ecx.images-amazon.com/images/G/02/gno/images/general/navAmazonLogoFooter._V192252709_.gif'] 
3

你不应该使用正则表达式解析HTML为this answer概述的各种原因。你应该使用HTML parser

0

有多种方式,你可以使用捕获组

path=("[^"]+") 

或回顾后语法

(?<=path=)"[^"]+" 

大概有其他一些选择也是如此。无论哪种方式,你应该像前面提到的海报可能使用一个HTML解析器的工作。不过,如果你使用正则表达式,你可能需要首先提取img标签,然后运行上面的正则表达式之一。

相关问题