Python 如何使用beautifulsoup从表中创建字典?

Python 如何使用beautifulsoup从表中创建字典?,python,beautifulsoup,scrapy,Python,Beautifulsoup,Scrapy,我试图通过beautifulsoup从表中检索数据,但不知何故,我的(初学者)语法错误: from bs4 import BeautifulSoup import requests main_url = "https://www.apo-in.de/product/acc-akut-600-brausetabletten.24170.html" req = requests.get(main_url) soup = BeautifulSoup(req.text, "html.parser")

我试图通过beautifulsoup从表中检索数据,但不知何故,我的(初学者)语法错误:

from bs4 import BeautifulSoup
import requests

main_url = "https://www.apo-in.de/product/acc-akut-600-brausetabletten.24170.html"

req = requests.get(main_url)
soup = BeautifulSoup(req.text, "html.parser")

title = soup.find("div", id = "accordionContent5e95581b6e244")

results = {}
for row in title.findAll('tr'):
     aux = row.findAll('td')
     results[aux[0].string] = aux[1].string

print(results)
以下是相关代码:

<div id="accordionContent5e95581b6e244" class="panel-collapse collapse in"> 
    <div class="panel-body"> 
        <table class="table" width="100%"> 
            <tbody>
                <tr> 
                    <th width="170">PZN</th>
                    <td>00520917</td> 
                </tr> 
                <tr> 
                    <th width="170">Anbieter</th> 
                    <td>Hexal AG</td>
                </tr>

PZN
00520917
安比特
德国赫素制药集团
我的目标是从
th td
单元格中检索词典


如何在beautifulsoup中实现这一点?

我建议使用
pandas
数据框中存储数据,然后导入到
字典中

import pandas as pd
from bs4 import BeautifulSoup
import requests

main_url = "https://www.apo-in.de/product/acc-akut-600-brausetabletten.24170.html"

req = requests.get(main_url)
soup = BeautifulSoup(req.text, "html.parser")
table=soup.select_one(".panel-body >table")
df=pd.read_html(str(table))[0]
print(df.set_index(0).to_dict('dict'))
输出

{1: {'Rezeptpflichtig': 'nein', 'Anbieter': 'Hexal AG', 'PZN': '00520917', 'Darreichungsform': 'Brausetabletten', 'Wirksubstanz': 'Acetylcystein', 'Monopräparat': 'ja', 'Packungsgröße': '40\xa0St', 'Apothekenpflichtig': 'ja', 'Produktname': 'ACC akut 600mg Hustenlöser'}}
  • 第一个错误:您使用的是
    id
    ,您可能希望刮取更多页面
  • 第二个错误:
    aux=row.findAll('td')
    这将返回一个项目的列表,因为您没有考虑表示
    aux[1]的
    th
    标记。字符串将引发异常
  • 代码如下:

    from bs4 import BeautifulSoup
    import requests
    
    main_url = "https://www.apo-in.de/product/acc-akut-600-brausetabletten.24170.html"
    
    req = requests.get(main_url)
    soup = BeautifulSoup(req.text, "html.parser")
    
    title = soup.find("div", class_="panel-collapse collapse in")
    
    results = {}
    for row in title.findAll('tr'):
        key   = row.find('th')
        value = row.find('td')
        results[key.text] =value.text.strip()
    print(results)
    
    输出:

    {'PZN': '00520917', 'Anbieter': 'Hexal AG', 'Packungsgröße': '40\xa0St', 'Produktname': 'ACC akut 600mg Hustenlöser', 'Darreichungsform': 'Brausetabletten', 'Monopräparat': 'ja', 'Wirksubstanz': 'Acetylcystein', 'Rezeptpflichtig': 'nein', 'Apothekenpflichtig': 'ja'}
    

    这回答了你的问题吗?直到现在。我尝试应用该示例,但确实得到了错误:来自bs4导入BeautifulSoup导入错误:在“bs4”中出现了错误的幻数:b'\x03\xf3\r\n'出现了什么问题?我可以尝试迭代所有tr和td。对于title.findAll('tr'):对于row.findAll('td'):results[aux[0].string]=aux[1]。string在html中是否有多个“td”?如果没有,为什么要使用“findAll函数”?每个tr中只有一个td。我使用scrapy normaly,现在第一次尝试使用BS4,无法运行它,因为执行test.py文件会导致上述导入错误。感谢您帮助我开始这个话题。@merlin这是一个完全无关的错误。您的bs4安装工作不正常。看见