【问题标题】:Beautiful Soup Error: Trying to retrieve data from web page returns empty arrayBeautiful Soup 错误:尝试从网页中检索数据返回空数组
【发布时间】:2019-04-07 14:11:15
【问题描述】:

我正在尝试使用 beautiful soupthis web page 下载投票意向民意调查列表。但是,我编写的代码返回一个空数组或什么都不返回。我使用的代码如下:

页面代码是这样的:

<div class="ST-c2-dv1 ST-ch ST-PS" style="width:33px"></div>
    <div class="ST-c2-dv2">41.8</div>

这就是我尝试过的:

import requests
from bs4 import BeautifulSoup

request = requests.get(quote_page) # take the page link
page = request.content  # extract page content

soup = BeautifulSoup(page, "html.parser")

# extract all the divs
for each_div in soup.findAll('div',{'class':'ST-c2-dv2'}):
    print each_div

此时,它什么也不打印。 我也试过这个:

tutti_a = soup.find_all("html_element", class_="ST-c2-dv2")

还有:

tutti_a = soup.find_all("div", class_="ST-c2-dv2")

但是我得到一个空数组 [] 或者什么都没有

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup


    【解决方案1】:

    我认为你可以使用以下网址

    import requests
    from bs4 import BeautifulSoup as bs
    import pandas as pd
    r = requests.get('https://www.marktest.com/wap/a/sf/v~[73D5799E1B0E]/name~Dossier_5fSondagensLegislativas_5f2011.HighCharts.Sondagens.xml.aspx')
    soup = bs(r.content, 'lxml')
    
    results = []
    for record in soup.select('p'):
        results.append([item.text for item in record.select('b')])
    df = pd.DataFrame(results)
    print(df)
    

    第 5、6、7、8、9、10 列对应于 PS、PSD、CDS、CDU、Bloco、Outros/Brancos/Nulos

    您可以删除不需要的列,添加适当的标题等。

    【讨论】:

    • 您好,感谢您的回复。我试图与@Attilio 一起解决同样的问题。运行这段代码后,我收到以下错误:FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?。我尝试pip install lxml,但问题仍然存在。我能做些什么?谢谢
    • 将解析器更改为 'html.parser' 而不是 lxml。让我知道这是否有效。否则还有'html5lib'
    猜你喜欢
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多