0
我嘗試使用python讀取RSS提要。我用feedparser,和它運作良好,爲每個供稿,除了一個:https://www.frenchweb.fr/feedFeedparser返回一個SAXParseException
import feedparser
feed = feedparser.parse("https://www.frenchweb.fr/feed")
print(feed)
輸出:
{
'feed': {},
'entries': [],
'bozo': 1,
'headers': {
'Date': 'Mon, 09 Oct 2017 08:04:31 GMT',
'Server': 'Apache',
'Vary': 'Cookie,Accept-Encoding',
'Expires': 'Thu, 19 Nov 1981 08:52:00 GMT',
'Cache-Control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
'Pragma': 'no-cache',
'ETag': '"2be89b4a462dd6d1652745b6e69cfdac"',
'X-UA-Compatible': 'IE=edge',
'Link': '<https://www.frenchweb.fr/wp-json/>; rel="https://api.w.org/"',
'Content-Encoding': 'gzip',
'Content-Length': '13440',
'Connection': 'close',
'Content-Type': 'application/rss+xml; charset="UTF-8"'
},
'etag': '"2be89b4a462dd6d1652745b6e69cfdac"',
'href': 'https://www.frenchweb.fr/feed',
'status': 200,
'encoding': 'UTF-8',
'version': '',
'bozo_exception': SAXParseException('XML or text declaration not at start of entity',),
'namespaces': {}
}
如果在相同的代碼,我只是做了簡單的GET,它的作品,我看內容:
web_page = requests.get(url, headers=headers, allow_redirects=True)
soup = BeautifulSoup(web_page.content, "html.parser")
print(soup)
輸出:
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/">
<channel>
<title>FrenchWeb.fr</title>
...
任何線索?
大,它的工作!感謝您的幫助日誌! – iero