<p class="result"><span class="bold">Final result </span><strong>107:74</strong> (22:16, 26:16, 31:24, 28:18)</p>对于上面的标记,我首先使用以下代码在循环中收集最终结果:
final_result = soup.find('p', class_='result')它工作得很好。但是,当我尝试使用类似的代码从下面的p标记中获取日期时,我总是得不到任何返回。每个类名在date日期之后的t数字中间都有稍微不同的数字,所以我尝试在soup.find class_中只使用date,但我尝试的都不起作用。
<p class="date datet t1594281600-4-1-1-1">Today, 09 Jul 2020, 09:00</p>完整的html示例可以在这里找到:https://www.oddsportal.com/basketball/china/cba/zhejiang-chouzhou-sichuan-tKo8L67d/
发布于 2020-07-09 22:07:30
使用class_属性会很复杂。
您可以尝试使用select方法,如下所示:
soup.select('p.date')
发布于 2020-07-09 22:26:46
嗯,看起来包含日期的标记是在页面加载之后填充的。但是,信息包含在class属性中并存储为纪元时间(1594281600相当于GMT时区中的Thursday 9 July 2020 08:00:00 )。
下面是你处理它的方法:
import requests
from bs4 import BeautifulSoup
from datetime import datetime
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
}
response = requests.get('https://www.oddsportal.com/basketball/china/cba/zhejiang-chouzhou-sichuan-tKo8L67d/', headers=headers)
tag_date = soup.find('p',{'class':'datet'})
# Extract the class name and get the attribute that contains the epoch time
epoch_time = tag_date.get('class')[-1].split('-')[0].replace('t','')
converted_time = datetime.fromtimestamp(int(epoch_time)).strftime("%Y-%m-%d %H:%M:%S")
print(converted_time)输出
'2020-07-09 10:00:00‘
似乎有一个时区参数,您可能需要处理,但大意在这里。
https://stackoverflow.com/questions/62816154
复制相似问题