【发布时间】:2019-08-31 07:25:47
【问题描述】:
我正在编写一个简单的网络爬虫来提取 ncaa 篮球比赛的比赛时间。代码不需要漂亮,只需工作即可。我已经从同一页面上的其他 span 标签中提取了值,但由于某种原因,我无法让这个标签正常工作。
from bs4 import BeautifulSoup as soup
import requests
url = 'http://www.espn.com/mens-college-basketball/game/_/id/401123420'
response = requests.get(url)
soupy = soup(response.content, 'html.parser')
containers = soupy.findAll("div",{"class" : "team-container"})
for container in containers:
spans = container.findAll("span")
divs = container.find("div",{"class": "record"})
ranks = spans[0].text
team_name = spans[1].text
team_mascot = spans[2].text
team_abbr = spans[3].text
team_record = divs.text
time_container = soupy.find("span", {"class":"time game-time"})
game_times = time_container.text
refs_container = soupy.find("div", {"class" : "game-info-note__container"})
refs = refs_container.text
print(ranks)
print(team_name)
print(team_mascot)
print(team_abbr)
print(team_record)
print(game_times)
print(refs)
我关心的具体代码是这个,
time_container = soupy.find("span", {"class":"time game-time"})
game_times = time_container.text
我只是提供了其余的代码来显示其他跨度标签上的 .text 有效。时间是我真正想要的唯一数据。我只是得到一个空字符串,其中包含我的代码当前的状态。
这是我调用 time_container 时得到的代码输出
<span class="time game-time" data-dateformat="time1" data-showtimezone="true"></span>
或者当我做 game_times 时只是 ''。
这是来自网站的 HTML 行:
<span class="time game-time" data-dateformat="time1" data-showtimezone="true">6:10 PM CT</span>
我不明白为什么运行脚本时下午 6:10 消失了。
【问题讨论】:
-
如果这是由 javascript 填充的,您将无法使用
BeautifulSoup获取它。
标签: python html web-scraping beautifulsoup