<p>对来自<code>urllib.urlopen</code>对象的源的检查表明该站点是动态的,因为找不到具有<code>table-row</code>类的更新的div对象。因此,您需要使用浏览器操作工具,如<code>selenium</code>:</p>
<pre><code>from bs4 import BeautifulSoup as soup
import re
import urllib
from selenium import webdriver
d = webdriver.Chrome()
classes = ['position', 'chase', 'car-number', 'driver', 'manufacturer', 'start-position not-mobile', 'laps not-mobile', 'laps-led not-mobile', 'final-status', 'points not-mobile', 'bonus not-mobile']
d.get('https://www.nascar.com/results/race_center/2018/monster-energy-nascar-cup-series/auto-club-400/stn/race/')
new_data = [filter(None, [b.text for b in i.find_all('div', {'class':re.compile('|'.join(classes))})]) for i in soup(d.page_source, 'lxml').find_all('div', {'class':'table-row'})]
</code></pre>
<p>输出:</p>
^{pr2}$
<p>编辑:要安装selenium,请运行<code>pip install selenium</code>,然后为您的浏览器安装适当的绑定:</p>
<p>Chrome驱动程序:<a href="https://sites.google.com/a/chromium.org/chromedriver/downloads" rel="nofollow noreferrer">https://sites.google.com/a/chromium.org/chromedriver/downloads</a></p>
<p>Firefox驱动程序:<a href="https://github.com/mozilla/geckodriver/releases" rel="nofollow noreferrer">https://github.com/mozilla/geckodriver/releases</a></p>
<p>然后,要运行代码,请创建一个具有与所选浏览器对应的类名的驱动程序对象,并将路径传递给驱动程序:</p>
<pre><code>d = webdriver.Firefox("/path/to/driver")
</code></pre>
<p>或者</p>
<pre><code>d = webdriver.Chrome("/path/to/driver")
</code></pre>
<p><strong>编辑</strong></p>
<p>将数据写入csv:</p>
<pre><code>import csv
write = csv.writer(open('nascarDrivers.csv', 'w'))
write.writerows(new_data) #new_data is the list of lists containing the table data
</code></pre>