首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试使for循环在命中html表中的特定行时中断。

尝试使for循环在命中html表中的特定行时中断。
EN

Stack Overflow用户
提问于 2018-07-22 06:08:46
回答 1查看 71关注 0票数 1

我正在尝试从我的代码中找到的wesbite中抓取一个webtable。基本上,我只想抓取今天的比赛,当我的for循环到达HTML表中包含第二天比赛信息的那部分时,它会停止。我试过用谷歌搜索这个,但似乎还是解决不了这个问题。任何帮助都将不胜感激。我的代码发布在下面。

代码语言:javascript
复制
url='http://www.oddsportal.com/baseball/usa/mlb/'
driver = webdriver.Chrome() 
driver.get(url)
time.sleep(5)

driver.find_element_by_id('user-header-timezone-expander').click() #get to est timezone
time.sleep(2)
driver.find_element_by_xpath("//*[contains(text(), 'GMT - 4')]").click() #get to est timezone
time.sleep(2)

content=driver.page_source

soup=BeautifulSoup(content,'lxml')


file_dates = []
todays_games=soup.find('table',{'class':'table-main'})
dummy_row=soup.find_all(attrs={'class':'table-dummyrow'})

for games in todays_games.select('td.table-time.datet'): #gets the time of the game
    games= [games.text]
    file_dates.append(games)

    if dummy_row==dummy_row[1]: #I want the for loop to break when it hits the gray header titled "Tomorrow, 22 Jul" on the webpage
        break

print(file_dates)  #still returns every game on the website though
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-22 06:34:34

要获得只有今天的比赛时间,您可以尝试以下代码:

代码语言:javascript
复制
games = [td.text for td in driver.find_elements_by_xpath('//table[@id="tournamentTable"]//td[contains(@class, "datet") '
                                                     'and following::span[starts-with(., "Tomorrow,")]]')]
print(games)

如果您仍然想使用bs4,请尝试:

代码语言:javascript
复制
file_dates = []
todays_games=soup.find('table',{'class':'table-main'})

for games in todays_games.select('tr')[2:]:
    if games.select('td.datet'):
        file_dates.append(games.select('td.datet')[0].text)
    if games.select('th'):
        break
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51460504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档