【发布时间】:2016-03-22 19:54:30
【问题描述】:
我想编辑一个使用 re.compile 来抓取数据的 Kodi 插件,并改为使用 BeautifulSoup4。
原来的代码是这样的:
import urllib, urllib2, re, sys, xbmcplugin, xbmcgui
link = read_url(url)
match = re.compile('<a class="frame[^"]*"'
' href="(http://somelink.com/section/[^"]+)" '
'title="([^"]+)">.*?<img src="([^"]+)".+?Length:([^<]+)',
re.DOTALL).findall(link)
for url, name, thumbnail, length in match:
addDownLink(name + length, url, 2, thumbnail)
它正在抓取的 HTML 是这样的:
<div id="content">
<span class="someclass">
<span class="sec">
<a class="frame" href="http://somlink.com/section/name-here" title="name here">
<img src="http://www.somlink.com/thumb/imgsection/thumbnail.jpg" >
</a>
</span>
<h3 class="title">
<a href="http://somlink.com/section/name-here">name here</a>
</h3>
<span class="details"><span class="length">Length: 99:99</span>
</span>
.
.
.
</div>
如何使用BeautifulSoup4 获取所有url (href)、name、length 和thumbnail,并将它们添加到addDownLink(name + length, url, 2, thumbnail) 中?
【问题讨论】:
-
你到底想得到什么,为什么要使用如此复杂的正则表达式?
-
@PadraicCunningham 不是我在使用这个复杂的正则表达式,我试图将其转换为 BeautifulSoup4 以使其变得简单,你能帮我吗??
-
有我们可以使用的类名吗?或者实际匹配的要求是什么?
-
@PadraicCunningham 我想要
name here作为名称,http://somlink.com/section/name-here作为 url,http://www.somlink.com/thumb/imgsection/thumbnail.jpg作为缩略图,99:99作为长度,所以它是addDownLink(name + length, url, 2, thumbnail)在 kodi -
有多个匹配项还是只有一个匹配项?我们可以使用带有
class="someclass"的span 作为查看的地方吗?
标签: python beautifulsoup xbmc