用“div”刮桌子

2024-04-28 06:43:56 发布

您现在位置:Python中文网/ 问答频道 /正文

当尝试刮取网页时,此表没有<tr>标记,全部是<div>标记。在

我要抓取的站点检查员如下所示: inspector screenshot

我希望能够从table-row类获取信息,但是scrape从不返回任何信息。使用下面的代码,当我刮取.table-header,或者仅仅是.practiceDataTable,我就能够从中获得数据。在

import bs4
import requests

res = requests.get('https://www.nascar.com/results/race_center/2018/monster-energy-nascar-cup-series/auto-club-400/stn/race/')

soup = bs4.BeautifulSoup(res.text, 'lxml')

soup.select('.nrwgt-lbh .practiceDataTable')

for i in soup.select('.nrwgt-lbh .practiceDataTable .table-row'):
    print(i.text)

我还注意到在inspector中,类“practiceDataTable”后面有一个空格,然后是“dataTable”,但是当我在代码中的任何地方使用它时,代码就不起作用了。在


Tags: 代码text标记importinspectortableresrequests
2条回答

对来自urllib.urlopen对象的源的检查表明该站点是动态的,因为找不到具有table-row类的更新的div对象。因此,您需要使用浏览器操作工具,如selenium

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'})]

输出:

^{pr2}$

编辑:要安装selenium,请运行pip install selenium,然后为您的浏览器安装适当的绑定:

Chrome驱动程序:https://sites.google.com/a/chromium.org/chromedriver/downloads

Firefox驱动程序:https://github.com/mozilla/geckodriver/releases

然后,要运行代码,请创建一个具有与所选浏览器对应的类名的驱动程序对象,并将路径传递给驱动程序:

d = webdriver.Firefox("/path/to/driver")

或者

d = webdriver.Chrome("/path/to/driver")

编辑

将数据写入csv:

import csv
write = csv.writer(open('nascarDrivers.csv', 'w'))
write.writerows(new_data) #new_data is the list of lists containing the table data

如果要从每个表行中获取文本,可以执行以下操作:

import bs4
import requests

res = requests.get('https://www.nascar.com/results/race_center/2018/monster-energy-nascar-cup-series/auto-club-400/stn/race/')

soup = bs4.BeautifulSoup(res.text, 'lxml')
tds = soup.find_all('div', class_='table-row')
for td in tds:
    print(td.text)

相关问题 更多 >