Python、BeautifulSoup或LXML—使用CSS标记从HTML解析图像URL
我到处寻找关于BeautifulSoup或LXML工作原理的合理解释。诚然,他们的文档很棒,但对于像我这样的python/编程新手来说,很难理解我在寻找什么 无论如何,作为我的第一个项目,我正在使用Python解析帖子链接的RSS提要——我已经用Feedparser完成了这项工作。我的计划是,然后刮每个帖子的图片。但就我的一生而言,我不知道如何让BeautifulSoup或LXML做我想做的事情!我花了几个小时阅读文档,在谷歌上搜索也没有用,所以我在这里。下面是一行从大图片我刮Python、BeautifulSoup或LXML—使用CSS标记从HTML解析图像URL,python,image,parsing,beautifulsoup,lxml,Python,Image,Parsing,Beautifulsoup,Lxml,我到处寻找关于BeautifulSoup或LXML工作原理的合理解释。诚然,他们的文档很棒,但对于像我这样的python/编程新手来说,很难理解我在寻找什么 无论如何,作为我的第一个项目,我正在使用Python解析帖子链接的RSS提要——我已经用Feedparser完成了这项工作。我的计划是,然后刮每个帖子的图片。但就我的一生而言,我不知道如何让BeautifulSoup或LXML做我想做的事情!我花了几个小时阅读文档,在谷歌上搜索也没有用,所以我在这里。下面是一行从大图片我刮 <div
<div class="bpBoth"><a name="photo2"></a><img src="http://inapcache.boston.com/universal/site_graphics/blogs/bigpicture/shanghaifire_11_22/s02_25947507.jpg" class="bpImage" style="height:1393px;width:990px" /><br/><div onclick="this.style.display='none'" class="noimghide" style="margin-top:-1393px;height:1393px;width:990px"></div><div class="bpCaption"><div class="photoNum"><a href="#photo2">2</a></div>In this photo released by China's Xinhua news agency, spectators watch an apartment building on fire in the downtown area of Shanghai on Monday Nov. 15, 2010. (AP Photo/Xinhua) <a href="#photo2">#</a><div class="cf"></div></div></div>
查找具有该css类的所有实例。嗯,它不会返回任何东西。我肯定我忽略了一些琐碎的事情,所以我非常感谢你的耐心
非常感谢您的回复
对于未来的谷歌用户,我将包括我的feedparser代码:
#! /usr/bin/python
# RSS Feed Parser for the Big Picture Blog
# Import applicable libraries
import feedparser
#Import Feed for Parsing
d = feedparser.parse("http://feeds.boston.com/boston/bigpicture/index")
# Print feed name
print d['feed']['title']
# Determine number of posts and set range maximum
posts = len(d['entries'])
# Collect Post URLs
pointer = 0
while pointer < posts:
e = d.entries[pointer]
print e.link
pointer = pointer + 1
您发布的代码将查找具有bpImage类的所有a元素。但是您的示例在img元素上有bpImage类,而不是a。您只需执行以下操作:
soup.find("img", { "class" : "bpImage" })
使用lxml,您可以执行以下操作:
import feedparser
import lxml.html as lh
import urllib2
#Import Feed for Parsing
d = feedparser.parse("http://feeds.boston.com/boston/bigpicture/index")
# Print feed name
print d['feed']['title']
# Determine number of posts and set range maximum
posts = len(d['entries'])
# Collect Post URLs
for post in d['entries']:
link=post['link']
print('Parsing {0}'.format(link))
doc=lh.parse(urllib2.urlopen(link))
imgs=doc.xpath('//img[@class="bpImage"]')
for img in imgs:
print(img.attrib['src'])
使用pyparsing搜索标记非常直观:
from pyparsing import makeHTMLTags, withAttribute
imgTag,notused = makeHTMLTags('img')
# only retrieve <img> tags with class='bpImage'
imgTag.setParseAction(withAttribute(**{'class':'bpImage'}))
for img in imgTag.searchString(html):
print img.src
哈哈。当然这将返回带有标记的url。有没有什么方法可以将这些内容简化为url?这太完美了。非常感谢你。
from pyparsing import makeHTMLTags, withAttribute
imgTag,notused = makeHTMLTags('img')
# only retrieve <img> tags with class='bpImage'
imgTag.setParseAction(withAttribute(**{'class':'bpImage'}))
for img in imgTag.searchString(html):
print img.src