Python 如何使用beautiful soup刮取标记属性列表?

Python 如何使用beautiful soup刮取标记属性列表?,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我试图在xml文件中获取标记属性列表。但是,我未能抓取标记属性列表 例如,我的数据看起来像 <sentences> <sentence id="1126814:0"> <Opinions> <Opinion target="Leon" category="RESTAURANT#GENERAL" polarity="positive" from="0" to="4"/> <Opinion target="Leon" ca

我试图在
xml
文件中获取标记属性列表。但是,我未能抓取标记属性列表

例如,我的数据看起来像

<sentences>    
<sentence id="1126814:0">
<Opinions>
    <Opinion target="Leon" category="RESTAURANT#GENERAL" polarity="positive" from="0" to="4"/>
    <Opinion target="Leon" category="AMBIENCE#GENERAL" polarity="positive" from="0" to="4"/>
    <Opinion target="specials" category="FOOD#QUALITY" polarity="positive" from="95" to="103"/>
    <Opinion target="atmosphere" category="AMBIENCE#GENERAL" polarity="positive" from="123" to="133"/>
    <Opinion target="French bistro fare" category="FOOD#QUALITY" polarity="positive" from="70" to="88"/>
    </Opinions>
</sentence>
</sentences>
我尝试了以下代码:

from bs4 import BeautifulSoup
with open("new2.xml", "r") as file:
    url = file.read()
soup = BeautifulSoup(url, "html.parser")
required0 = soup.find_all("sentences")

text1 = []
for i in required0:
    sent_id = []
    category = []
    for sentences in soup.find_all('sentence'):
        if sentences.has_attr('id'):
            sent_id.append(sentences['id'])
        text.append(sentences.get_text())
    for opinions in soup.find_all('opinion'):
        cat1 = []
        pola1 = []
        targ = []
        if opinions.has_attr('category'):
            cat1.append(opinions['category'])
        if opinions.has_attr('polarity'):
            pola1.append(opinions['polarity'])
        if opinions.has_attr('target'):
            targ.append(opinions['target'])
        cat_list = list(zip(cat1,pola1,targ))
        category.append(cat_list)
    catlist= list(zip(sent_id, category, category))
    text1.append(catlist)
我得到如下输出:

[[('1126814:0',
   [('RESTAURANT#GENERAL', 'positive', 'Leon')],
   [('RESTAURANT#GENERAL', 'positive', 'Leon')])]]

提前感谢您的帮助。

这里是我用来获取所有链接的解决方案。这是您的解决方案的修改版本

from bs4 import BeautifulSoup

data = """<sentences>    
<sentence id="1126814:0">
<Opinions>
    <Opinion target="Leon" category="RESTAURANT#GENERAL" polarity="positive" from="0" to="4"/>
    <Opinion target="Leon" category="AMBIENCE#GENERAL" polarity="positive" from="0" to="4"/>
    <Opinion target="specials" category="FOOD#QUALITY" polarity="positive" from="95" to="103"/>
    <Opinion target="atmosphere" category="AMBIENCE#GENERAL" polarity="positive" from="123" to="133"/>
    <Opinion target="French bistro fare" category="FOOD#QUALITY" polarity="positive" from="70" to="88"/>
    </Opinions>
</sentence>"""

soup = BeautifulSoup(data, "html.parser")
sentences = soup.find_all("sentence")
result = []
for sent in sentences:
    element = []
    if sent.has_attr("id"):
        element.append(sent["id"])
    opinions = sent.find_all("opinion")
    for op in opinions:
        el = []
        if op.has_attr("category"):
            el.append(op["category"])
        if op.has_attr("polarity"):
            el.append(op["polarity"])
        if op.has_attr("target"):
            el.append(op["target"])
        element.append(el)
    result.append(tuple(element))
print(result)

请让我知道这是否是您想要的答案格式

请描述您的问题是什么?你需要什么帮助?
from bs4 import BeautifulSoup

data = """<sentences>    
<sentence id="1126814:0">
<Opinions>
    <Opinion target="Leon" category="RESTAURANT#GENERAL" polarity="positive" from="0" to="4"/>
    <Opinion target="Leon" category="AMBIENCE#GENERAL" polarity="positive" from="0" to="4"/>
    <Opinion target="specials" category="FOOD#QUALITY" polarity="positive" from="95" to="103"/>
    <Opinion target="atmosphere" category="AMBIENCE#GENERAL" polarity="positive" from="123" to="133"/>
    <Opinion target="French bistro fare" category="FOOD#QUALITY" polarity="positive" from="70" to="88"/>
    </Opinions>
</sentence>"""

soup = BeautifulSoup(data, "html.parser")
sentences = soup.find_all("sentence")
result = []
for sent in sentences:
    element = []
    if sent.has_attr("id"):
        element.append(sent["id"])
    opinions = sent.find_all("opinion")
    for op in opinions:
        el = []
        if op.has_attr("category"):
            el.append(op["category"])
        if op.has_attr("polarity"):
            el.append(op["polarity"])
        if op.has_attr("target"):
            el.append(op["target"])
        element.append(el)
    result.append(tuple(element))
print(result)
[('1126814:0', ['RESTAURANT#GENERAL', 'positive', 'Leon'], ['AMBIENCE#GENERAL', 'positive', 'Leon'], ['FOOD#QUALITY', 'positive', 'specials'], ['AMBIENCE#GENERAL', 'positive', 'atmosphere'], ['FOOD#QUALITY', 'positive', 'French bistro fare'])]