擅长:python、mysql、java
<p>他们将日期时间嵌入到data date属性中,因此另一个选择不是求助于selenium,而是简单地将该属性提取出来并使用beauthulsoup将其粘贴到td元素中。在</p>
<pre><code>from bs4 import BeautifulSoup
import requests
import pandas as pd
import dateutil
from datetime import datetime
espn_page = requests.get('https://www.espn.co.uk/nfl/fixtures/_/week/2/seasontype/1')
soup = BeautifulSoup(espn_page.content, 'html.parser')
espn_schedule = soup.find('div', {'class': 'main-content'})
for td in espn_schedule.find_all('td', {'data-behavior': 'date_time'}):
utc = dateutil.parser.parse(td.get('data-date'))
localtime = utc.astimezone(dateutil.tz.gettz())
td.string = localtime.strftime("%I:%M")
df = pd.read_html(str(espn_schedule))
print(df[0].columns)
print(df[0][df[0].columns[2]])
</code></pre>