【问题标题】:Can't parse xml properly with BeautifulSoup无法使用 BeautifulSoup 正确解析 xml
【发布时间】:2021-04-23 06:45:07
【问题描述】:

我正在尝试抓取此页面:https://www.france24.com/en/europe/rss

我的代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup

xml = urlopen("https://www.france24.com/en/europe/rss")
data = xml.read()
text = data.decode('utf-8')
bs = BeautifulSoup(text, "lxml")
items = bs.find("rss").find("channel").find_all("item")
for n, item in enumerate(items):
    print(f"\n{n+1} - {item.find('title').get_text()}")
    print(item.find("pubDate"))
    print(item.find("description").get_text().replace("\n", ""))
    print(item.find("link").get_text())

我感兴趣的结构:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
    <channel>
        <lastBuildDate>Fri, 23 Apr 2021 06:08:56 GMT</lastBuildDate>
        <item>
            <category>Europe</category>
            <title>French fishermen seek to block British shipments in Brexit protest</title>
            <link>https://www.france24.com/en/europe/20210423-french-fishermen-seek-to-block-british-shipments-in-brexit-protest</link>
            <description>
French trawlermen angered by the slow issuance of licenses to fish inside British waters after Brexit on Thursday blocked lorries carrying UK-landed fish as they arrived in Boulogne-sur-Mer, Europe’s largest seafood processing centre.
</description>
            <media:thumbnail url="https://s.france24.com/media/display/70e354e8-a3e7-11eb-a6eb-005056bf87d6/w:1024/p:16x9/Brexit%20fishermen%20protest.jpg" />
            <enclosure url="https://s.france24.com/media/display/70e354e8-a3e7-11eb-a6eb-005056bf87d6/w:1024/p:16x9/Brexit%20fishermen%20protest.jpg" type="image/jpeg" length="0" />
            <guid isPermaLink="false">cadc08aa-a3e7-11eb-91c0-005056bff4a8</guid>
            <pubDate>Fri, 23 Apr 2021 03:55:46 GMT</pubDate>
            <source url="https://s.france24.com/media/display/70e354e8-a3e7-11eb-a6eb-005056bf87d6/w:1024/p:16x9/Brexit%20fishermen%20protest.jpg">© Denis Charlet, AFP</source>
            <dc:creator>NEWS WIRES</dc:creator>
        </item>

输出:

1 - French fishermen seek to block British shipments in Brexit protest
None
French trawlermen angered by the slow issuance of licenses to fish inside British waters after Brexit on Thursday blocked lorries carrying UK-landed fish as they arrived in Boulogne-sur-Mer, Europe’s largest seafood processing centre.

(...)

如您所见,pubDate 和链接没有打印出来。

这是print(bs)的结果,看看BS如何解析xml(格式化):

<?xml version="1.0" encoding="UTF-8"?>
<html>
    <body>
        <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
            <channel>
                <lastbuilddate>Fri, 23 Apr 2021 06:08:56 GMT</lastbuilddate>
                <item>
                    <category>Europe</category>
                    <title>French fishermen seek to block British shipments in Brexit protest</title>
                    <link />
                    https://www.france24.com/en/europe/20210423-french-fishermen-seek-to-block-british-shipments-in-brexit-protest
                    <description>
French trawlermen angered by the slow issuance of licenses to fish inside British waters after Brexit on Thursday blocked lorries carrying UK-landed fish as they arrived in Boulogne-sur-Mer, Europe’s largest seafood processing centre.
</description>
                    <media:thumbnail url="https://s.france24.com/media/display/70e354e8-a3e7-11eb-a6eb-005056bf87d6/w:1024/p:16x9/Brexit%20fishermen%20protest.jpg"></media:thumbnail>
                    <enclosure length="0" type="image/jpeg" url="https://s.france24.com/media/display/70e354e8-a3e7-11eb-a6eb-005056bf87d6/w:1024/p:16x9/Brexit%20fishermen%20protest.jpg"></enclosure>
                    <guid ispermalink="false">cadc08aa-a3e7-11eb-91c0-005056bff4a8</guid>
                    <pubdate>Fri, 23 Apr 2021 03:55:46 GMT</pubdate>
                    <source url="https://s.france24.com/media/display/70e354e8-a3e7-11eb-a6eb-005056bf87d6/w:1024/p:16x9/Brexit%20fishermen%20protest.jpg">© Denis Charlet, AFP</source>
                    <dc:creator>NEWS WIRES</dc:creator>
                </item>

注意发布日期链接/

我看不出问题出在哪里。关于为什么解析不正确有什么意见吗?

【问题讨论】:

    标签: python web-scraping beautifulsoup lxml


    【解决方案1】:

    试试xml解析器:

    bs = BeautifulSoup(text, "xml")
    

    这是我为第一项得到的输出:

    1 - Without licenses to fish in British waters, French trawlermen block deliveries of UK-landed fish
    <pubDate>Fri, 23 Apr 2021 03:55:46 GMT</pubDate>
    French trawlermen angered by the slow issuance of licenses to fish inside British waters after Brexit on Thursday blocked lorries carrying UK-landed fish as they arrived in Boulogne-sur-Mer, Europe’s largest seafood processing centre.
    https://www.france24.com/en/europe/20210423-french-fishermen-seek-to-block-british-shipments-in-brexit-protest
    

    【讨论】:

      【解决方案2】:

      对于发布日期:

      在用 BS 解析的 Xml 中,应答器 pubDate 变为 pubdate 或在您的代码中您正在寻找 pubDate

      也许你可以试试这个。

      【讨论】:

        猜你喜欢
        • 2015-10-15
        • 1970-01-01
        • 2012-12-07
        • 1970-01-01
        • 2013-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多