Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用BeautifulSoup4解析数据?_Python_Xml_Parsing_Beautifulsoup - Fatal编程技术网

Python 如何使用BeautifulSoup4解析数据?

Python 如何使用BeautifulSoup4解析数据?,python,xml,parsing,beautifulsoup,Python,Xml,Parsing,Beautifulsoup,以下是.xml文件中的示例: <title>Kaufsignal für Marriott International</title> <link>https://insideparadeplatz.ch/2015/03/06/kaufsignal-fuer-marriott-international/</link> <pubDate>Fri, 06 Mar 2015 </pubDate>

以下是.xml文件中的示例:

    <title>Kaufsignal für Marriott International</title>
    <link>https://insideparadeplatz.ch/2015/03/06/kaufsignal-fuer-marriott-international/</link>
    <pubDate>Fri, 06 Mar 2015 </pubDate>
    <content:encoded>
        <![CDATA[
            <p class="p1">
                <span class="s1">Mit Marken wie Bulgari, Ritz-Carlton, Marriott und weiteren ist Marriott International nach sämtlichen Kriterien, die vom <a href="http://www.obermatt.com/de/home.html">
                <span class="s2">Obermatt-System</span></a></span> bewertet werden, ein interessantes Investment. Der Titel ist relativ gesehen günstig, das Unternehmen sollte weiter überproportional wachsen, und es ist solide finanziert, mit einem guten Verhältnis von Eigenkapital und Schulden. Über alle Kategorien gesehen landet die 
                <span class="s3">Marriott-Aktie</span></a>, die derzeit an der Technologiebörse Nasdaq bei rund 84 Dollar gehandelt wird, in der Wochenauswertung im Total-Ranking auf dem ersten Platz.

                <img class="aligncenter wp-image-17092 size-full" src="https://insideparadeplatz.ch/wp-content/uploads/2015/03/Total-Ranking-6-Mar-2015.png" alt="Total-Ranking 6 Mar 2015" width="873" height="627" /></a></p>]]>
    </content:encoded>
我试过:

for item in soup.find_all('item'):
    for data in item.find_all('content:encoded'):
        for img in data.find_all('img'):
            img_list.append(img.text)

但是什么都没有。我在这里遗漏了什么?

我想你在获取img数据时会遇到麻烦

for item in soup.find("content:encoded"):
   print(item)
   print(type(item))
然后看: 因此,bs4认为它是一个字符串,您需要手动解析它,或者将新字符串重新输入到新的bs4对象中

,最重要的是,您的标记不会对齐可能的重复项
for item in soup.find("content:encoded"):
   print(item)
   print(type(item))