从<img/>标记中提取路径和文件名

时间:2011-01-29 12:44:35

标签: python html html-parsing

我有一些网页的源代码,我需要查找所有出现的标记并提取该图片的名称和位置(我需要的例子<img src="../images/test.jpg" /> path="../images/"file="test.jpg")。我怎么能用正则表达式做到这一点?

3 个答案:

答案 0 :(得分: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']

答案 1 :(得分:3)

由于this answer中列出的各种原因,您不应使用正则表达式来解析HTML。您应该使用HTML parser

答案 2 :(得分:0)

有很多方法,你可以使用捕获组

path=("[^"]+")

或lookbehind语法

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

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