试图在Kodi抓一个网站获取个人脚本,我的代码正在运行,但当BS展示内容时,它仍然有标签。对于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
谢谢。
答案 0 :(得分:0)
这可能会有所帮助。我使用find_next
方法获得第二个范围
from bs4 import BeautifulSoup
d = """<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>"""
soup = BeautifulSoup(d, "html.parser")
print soup.find("li").span.find_next('span').get_text()
#or
print soup.find("li").find("span", {"style": "font-size:16px;color:#EFEFEF;"}).text
<强>输出:强>
04:30 - 05:30 The Tonight Show Starring Jimmy Fallon - Channel 34
答案 1 :(得分:0)
尝试以下方法。它应该能得到你想要的结果。
import requests
from bs4 import BeautifulSoup
res = requests.get("http://rushmore.tv/schedule")
soup = BeautifulSoup(res.text,"lxml")
for content in soup.select("#myUL span[style*='#EFEFEF']"):
print(content.text)
部分输出:
16:00 - 20:00 Tennis: ATP Dubai - 13 (720P / US) & 21 (720P / CA)
16:00 - 20:00 Tennis: ATP Dubai - 13 (720P / US)
17:00 - 21:00 Golic and Wingo - Channel 03
18:45 - 23:00 Snooker: Welsh Open - Channel 105
20:00 - 23:30 The Dan Patrick Show - Channel 11