4 回答

TA貢獻1880條經(jīng)驗 獲得超4個贊
你的matchscrape功能是錯誤的。而不是match.find返回第一項的函數(shù),您應該使用與matches函數(shù)match.findAll函數(shù)相同的方式。然后像下面的例子一樣遍歷找到的日期時間。
def matchscrape(g_data):
for match in g_data:
datetimes = match.findAll('div', class_='main time col-sm-2 hidden-xs')
for datetime in datetimes:
print("DateTimes; ", datetime.text.strip())
print('-' * 80)
第二件事是解析 html 頁面。該頁面是用 編寫的,html因此您可能應該使用BeautifulSoup(page.text, 'html.parser')而不是lxml

TA貢獻1851條經(jīng)驗 獲得超5個贊
我也只有 1 個時間戳。不過,還有其他可能導致問題的原因。在這種情況下,網(wǎng)站通常具有動態(tài)內(nèi)容,并且在某些情況下,這些內(nèi)容并不總是隨請求正確加載。
如果您真的確定問題是請求沒有正確獲取站點,請嘗試requests_html(pip install requests-html),這會打開一個肯定會加載所有動態(tài)內(nèi)容的會話:
from requests_html import HTMLSession
from bs4 import BeautifulSoup
session = HTMLSession()
request = session.get(LINK)
html = BeautifulSoup(request.text, "html.parser")

TA貢獻1757條經(jīng)驗 獲得超8個贊
今天只有一次,但您可以通過首先使用所需日期發(fā)出 POST 請求并重新加載頁面來獲得明天的時間。
例如:
import requests
from bs4 import BeautifulSoup
url = 'https://sport-tv-guide.live/live/darts'
select_date_url = 'https://sport-tv-guide.live/ajaxdata/selectdate'
with requests.session() as s:
# print times for today:
print('Times for today:')
soup = BeautifulSoup(s.get(url).content, 'html.parser')
for t in soup.select('.time'):
print(t.get_text(strip=True, separator=' '))
# select tomorrow:
s.post(select_date_url, data={'d': '2020-07-19'}).text
# print times for tomorrow:
print('Times for 2020-07-19:')
soup = BeautifulSoup(s.get(url).content, 'html.parser')
for t in soup.select('.time'):
print(t.get_text(strip=True, separator=' '))
印刷:
Times for today:
Darts 17:05
Times for 2020-07-19:
Darts 19:05
Darts 19:05

TA貢獻1799條經(jīng)驗 獲得超6個贊
在獲得上述有用的答案后,我能夠確定問題是網(wǎng)站上存儲了一個 cookie,其中包含用戶選擇的國家/地區(qū)信息,以顯示運動日程數(shù)據(jù)。在這個例子中,澳大利亞的一個頻道在 18:00 有一個列表。由于從請求模塊收到的請求沒有 cookie 數(shù)據(jù),這最初沒有通過我上面的代碼顯示在輸出中。
我能夠通過以下代碼提供必要的 cookie 信息
def makesoup(url):
cookies = {'mycountries' : '101,28,3,102,42,10,18,4,2'} # pass cookie data
r = requests.post(url, cookies=cookies)
return BeautifulSoup(r.text,"html.parser")
現(xiàn)在輸出了正確的信息
只需發(fā)布此答案,以防將來幫助遇到類似問題的人。
添加回答
舉報