<p>我刚试过,下面的代码对我有用。<code>items =</code>这句话太可怕了,请提前道歉。但现在它起作用了。。。在</p>
<p><strong>编辑</strong>
刚刚更新了代码段,您可以使用<code>ElementTree.iter('tag')</code>来迭代所有具有{<cd3>}的节点:</p>
<pre><code>import urllib.request
import xml.etree.ElementTree
news_url = "https://news.google.com/news/rss"
with urllib.request.urlopen(news_url) as page:
xml_page = page.read()
# Parse XML page
e = xml.etree.ElementTree.fromstring(xml_page)
# Get the item list
for it in e.iter('item'):
print(it.find('title').text)
print(it.find('link').text)
print(it.find('pubDate').text, '\n')
</code></pre>
<hr/>
<p><strong>EDIT2:Discussion图书馆的个人偏好</strong><br/>
就个人而言,对于那些我必须在其中做<em>工作的交互式/动态页面(单击此处,填写表单,获得结果,…):我使用<code>selenium</code>,而且通常我不需要使用<code>bs4</code>,因为您可以直接使用selenium来查找和解析您要查找的web的特定节点。在</p>
<p>我使用<code>bs4</code>和<code>requests</code>(而不是<code>urllib.request</code>)来<strong>在我不想安装整个webdriver的项目中解析更多静态网页</strong>。在</p>
<p>使用<code>urllib.request</code>没有什么错,但是<code>requests</code>(参见这里的<a href="http://docs.python-requests.org/en/master/" rel="nofollow noreferrer">docs</a>)是最好的python包之一(在我看来),它是如何创建一个简单而强大的API的好例子。在</p>