【问题标题】:xbmc/kodi python scrape data using BeautifulSoupxbmc/kodi python 使用 BeautifulSoup 抓取数据
【发布时间】: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


【解决方案1】:
from bs4 import BeautifulSoup

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>
"""

soup = BeautifulSoup(html, "lxml")
sec = soup.find("span", {"class": "someclass"})
# get a tag with frame class
fr = sec.find("a", {"class": "frame"})

# pull img src and href from the a/frame
url, img = fr["href"], fr.find("img")["src"]

# get h3 with title class and extract the text from the anchor
name =  sec.select("h3.title a")[0].text

# "size" is in the span with the details class
size = sec.select("span.details")[0].text.split(None,1)[-1]


print(url, img, name.strip(), size.split(None,1)[1].strip())

这给了你:

('http://somlink.com/section/name-here', 'http://www.somlink.com/thumb/imgsection/thumbnail.jpg', u'name here', u'99:99')

如果您有多个部分,我们只需要 find_all 并将逻辑应用于每个部分:

def secs():
    soup = BeautifulSoup(html, "lxml")
    sections = soup.find_all("span", {"class": "someclass"})
    for sec in sections:
        fr = sec.find("a", {"class": "frame"})
        url, img = fr["href"], fr.find("img")["src"]
        name, size =  sec.select("h3.title a")[0].text, sec.select("span.details")[0].text.split(None,1)[-1]
        yield url, name, img,size

如果您不了解所有课程,但您知道例如有一个 img 标签,您可以在该部分调用 find:

 sec.find("img")["src"]

同样的逻辑也适用于其余部分。

【讨论】:

  • 那么如何在kodi中使用addDownLink(name + length, url, 2, thumbnail)来处理所有class="someclass"?
  • 就像你问题中的 findall 一样迭代几秒
  • 谢谢你这么匹配我的问题,但你能告诉我如何只得到99:99而不是Length: 99:99
  • 谢谢你这么匹配我的问题,但你能告诉我如何只得到99:99而不是Length: 99:99
  • 是的,现在应该可以正常工作了,我最初添加了错误的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多