[我正在尝试从该网站https://www.solarreviews.com/solar-panels/solar-panel-cost/中提取表格,从每个州提取太阳能价格,并将其传递到CSV文件中。这是到目前为止的代码。它返回“无”。有人可以向我解释原因吗?怎么办?我将不胜感激。
from bs4 import BeautifulSoup as soup
import csv
#Open page and grab HTML
my_url = ('https://www.solarreviews.com/solar-panels/solar-panel-cost/')
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
#HTML parser
page_soup = soup(page_html, 'html.parser')
#Find table
ele_table = page_soup.find('table',{"class":"table table-hover stateAveragesTable"})
print(ele_table)```
您需要更改tr样式属性,以便全部显示。请注意,如果您提出过多的请求,您还将面临IP禁止的风险。
import requests,re
from bs4 import BeautifulSoup as bs
import pandas as pd
r = requests.get('https://www.solarreviews.com/solar-panels/solar-panel-cost/')
soup = bs(r.content, 'lxml')
table_html = str(soup.select_one('.stateAveragesTable'))
table_html = re.sub('display: none;','', table_html)
print(pd.read_html(str(table_html)))