【问题标题】:Why does my scraper return none with beautifulsoup?为什么我的刮刀没有返回美丽汤?
【发布时间】:2019-11-11 19:22:47
【问题描述】:

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

【问题讨论】:

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


【解决方案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)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-13
    • 2021-01-15
    • 2021-03-02
    • 2020-12-13
    • 2019-03-13
    • 2014-05-28
    • 2020-09-28
    相关资源
    最近更新 更多