用漂亮的汤刮WP表

时间:2020-05-18 12:51:56

标签: python beautifulsoup

我想刮一张桌子进行数据分析。但是我认为,我正在努力寻找表类。它返回我的对象​​没有属性“ tbody”。 任何帮助都会得到应用。

from bs4 import BeautifulSoup
import requests
import pandas as pd 

url = 'https://prosettings.net/rocket-league-pro-camera-settings-controller-list/'

response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

print(soup)

table = soup.find('table',{'class':'wpDataTables wpDataTablesWrapper'}).tbody
print(table)

在开发工具中,可以找到此表:

<div id="table_1_wrapper" class="wpDataTables wpDataTablesWrapper">
<table id="table_1" class="responsive display nowrap data-t data-t wpDataTable dataTable" style="" data-described-by="table_1_desc" data-wpdatatable_id="61" role="grid" aria-describedby="table_1_info">

1 个答案:

答案 0 :(得分:1)

没有table类别为wpDataTables wpDataTablesWrapper,但类别为wpDataTable(末尾没有s

table = soup.find('table', {'class': 'wpDataTable'}).tbody

这给了我tbody


编辑:作为上面评论中提到的@ r-初学者,您也可以使用id

table = soup.find('table', {'id': 'table_1'}).tbody

BTW::如果您在浏览器table中看到类wpDataTables wpDataTablesWrapper,则服务器可能为不同的设备/浏览器发送了不同的HTML,您可能必须使用{{1} }来模拟您的浏览器。默认情况下,User-Agent在标头requests中发送类似Python/x.x的内容

相关问题