为什么我的刮板没有美丽的汤归还?

时间:2019-11-10 22:13:41

标签: python html web-scraping beautifulsoup python-3.7

我正在尝试从该网站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)```

1 个答案:

答案 0 :(得分:1)

您需要更改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)))