美丽的汤找不到任何元素

时间:2020-01-02 17:09:34

标签: python-3.x

我只是从网络抓取开始。我在喝漂亮的汤时遇到麻烦。我尝试将div类也更改为其他类,但它始终返回[]。这是我的代码。

import time
from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:/Users/MuhIsmail/Downloads/cd79/chromedriver.exe")
url = "https://www.cricbuzz.com/cricket-match/live-scores"
driver.get(url)
driver.maximize_window()
time.sleep(4)
content = driver.page_source
soup = BeautifulSoup(content, "html.parser")
scores = soup.find_all('div', class_='col-xs-9 col-lg-9 dis-inline')
print(scores)

2 个答案:

答案 0 :(得分:1)

import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.cricbuzz.com/cricket-match/live-scores")
soup = BeautifulSoup(r.text, 'html.parser')


for item in soup.select("a.cb-mat-mnu-itm:nth-child(5)"):
    print(item.text)

输出:

MLR vs SYS - SYS Won

答案 1 :(得分:0)

返回[]是因为页面上没有该类的元素。

如果打开浏览器控制台并执行简单的操作

document.getElementsByClassName('col-xs-9 col-lg-9 dis-inline')

它将不返回任何结果。

我也尝试过:

import requests
from bs4 import BeautifulSoup

url = "https://www.cricbuzz.com/cricket-match/live-scores"
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
scores = soup.find_all('div', {'class':'col-xs-9 col-lg-9 dis-inline'})
print(scores)