Как анализировать данные с помощью BeautifulSoup4? - PullRequest
0 голосов
/ 14 мая 2018

Ниже приведен пример из 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>

То, что я пытаюсь сделать, с помощью beautifulsoup4, я могу извлечь 'title', 'link', 'pubDate'. Но проблема в том, что «контент: закодирован». Здесь я хочу извлечь 'img' из 'content: encoded' для моего 'img_list'. Я перепробовал много решений, но все, что я получил, это Ни один.

title = []
link = []
date = []
img_list = []
for item in soup.find_all('item'):
    for t in item.find_all('title'):
        title.append(t.text)
for item in soup.find_all('item'):
    for l in item.find_all('link'):
        link.append(t.text)
for item in soup.find_all('item'):
    for date in item.find_all('pubDate'):
        pubDate.append(date.text)
for item in soup.find_all('item'):
    for data in item.find_all('content:encoded'):
        data.text

Я пытался:

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)

но ничего не получил. Что мне здесь не хватает?

1 Ответ

0 голосов
/ 14 мая 2018

Думаю, у вас возникнут проблемы с выводом этих img-данных.

for item in soup.find("content:encoded"):
   print(item)
   print(type(item))

Затем посмотрите: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigablestring

Так что bs4 считает, что это строка, и вынеобходимо разобрать его вручную или, возможно, сослаться на новую строку в новый объект bs4

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...