【发布时间】:2018-02-28 12:49:40
【问题描述】:
试图在 Kodi 中为个人脚本抓取网站,我的代码正在运行,但是当 BS 呈现内容时,它仍然有标签。 Reletavily 是 Python 的新手,所以请寻找易于理解的答案。
当前输出:
<li>
<span style="font-family:trebuchet ms,helvetica,sans-serif;">
<span style="font-size:16px;color:#EFEFEF;">
04:30 - 05:30 The Tonight Show Starring Jimmy Fallon
<span style="color:#999999;">
- Channel 34
</span>
</span>
</span>
</li>
想要的输出:
04:30 - 05:30 The Tonight Show Starring Jimmy Fallon - Channel 34
我的代码:
import xbmcgui
import xbmcaddon
import urllib, urllib2, re, HTMLParser, os
from bs4 import BeautifulSoup
pg_source = ''
req = urllib2.Request('http://rushmore.tv/schedule')
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36')
try:
response = urllib2.urlopen(req)
pg_source = response.read().decode('utf-8' , 'ignore')
response.close()
except:
pass
content = []
soup = BeautifulSoup(pg_source)
content = BeautifulSoup(soup.find('ul', { 'id' : 'myUL' }).prettify())
xbmcgui.Dialog().textviewer(str(content), str(content))
xbmcgui.Window
谢谢。
【问题讨论】:
-
你试过
xpath吗? -
我没有试过 xpath 没有。你能详细说明一下吗?
-
只是提取
prettify不做的文本内容。 -
https://stackoverflow.com/questions/8692/how-to-use-xpath-in-python/13504511这里是一个如何使用它的例子。您可以将它与lxml或纯python 一起使用import xpath -
@KeyurPotdar 抱歉还在学习,谢谢。
标签: python html beautifulsoup kodi