将Beautiful Soup捕获的值存储在字典中,然后访问这些值

时间:2017-01-09 11:54:04

标签: python dictionary beautifulsoup store access

我正在学习Python中的美丽汤和词典。我正在遵循斯坦福大学美丽汤的简短教程,可以在这里找到:http://web.stanford.edu/~zlotnick/TextAsData/Web_Scraping_with_Beautiful_Soup.html

由于访问webside是禁止的,我已将教程中显示的文本存储到字符串中,然后将字符串汤转换为汤对象。打印输出如下:

print(soup_string)

<html><body><div class="ec_statements"><div id="legalert_title"><a    
href="/Legislation-and-Politics/Legislative-Alerts/Letter-to-Senators-
Urging-Them-to-Support-Cloture-and-Final-Passage-of-the-Paycheck-
Fairness-Act-S.2199">'Letter to Senators Urging Them to Support Cloture     
and Final Passage of the Paycheck Fairness Act (S.2199)
</a>
</div>
<div id="legalert_date">
September 10, 2014
</div>
</div>
<div class="ec_statements">
<div id="legalert_title">
<a href="/Legislation-and-Politics/Legislative-Alerts/Letter-to-  
Representatives-Urging-Them-to-Vote-on-the-Highway-Trust-Fund-Bill">
Letter to Representatives Urging Them to Vote on the Highway Trust Fund Bill
</a>
</div>
<div id="legalert_date">
        July 30, 2014
       </div>
</div>
<div class="ec_statements">
<div id="legalert_title">
<a href="/Legislation-and-Politics/Legislative-Alerts/Letter-to-Representatives-Urging-Them-to-Vote-No-on-the-Legislation-Providing-Supplemental-Appropriations-for-the-Fiscal-Year-Ending-Sept.-30-2014">
         Letter to Representatives Urging Them to Vote No on the Legislation Providing Supplemental Appropriations for the Fiscal Year Ending Sept. 30, 2014
        </a>
</div>
<div id="legalert_date">
        July 30, 2014
       </div>
</div>
<div class="ec_statements">
<div id="legalert_title">
<a href="/Legislation-and-Politics/Legislative-Alerts/Letter-to-Senators-Urging-Them-to-Vote-Yes-
             on-the-Motion-to-Proceed-to-the-Emergency-Supplemental-Appropriations-Act-of-2014-S.2648"></a></div></div></body></html>

在某些时候,导师会捕获汤对象中具有Tag&#34; div&#34;,class _ =&#34; ec_statements&#34;。

的所有元素。
   letters = soup_string.find_all("div", class_="ec_statements")

然后导师说:

&#34;我们将查看我们的信件集中的所有项目,并为每个项目提取名称并将其作为我们字典中的密钥。该值将是另一个字典,但我们还没有找到其他项目的内容,所以我们只是创建一个空的字典对象。&#34;

代码如下:

lobbying = {}
for element in letters:
    lobbying[element.a.get_text()] = {}

然而,当我打印游说词典的键和值时,我发现了最后一个元素 - &#34;写给参议员的信 - 催促他们投票 - 是 - 动态 - -Proceed到最紧急补充-拨款-ACT-的-2014-S.2648&#34; - 失踪。相反,有一个没有分配密钥的空字典。

for key, value in lobbying.iteritems():
    print key, value

{}

         Letter to Representatives Urging Them to Vote No on the Legislation Providing Supplemental Appropriations for the Fiscal Year Ending Sept. 30, 2014
         {}

         Letter to Representatives Urging Them to Vote on the Highway Trust Fund Bill
         {}
'Letter to Senators Urging Them to Support Cloture and Final Passage of the Paycheck Fairness Act (S.2199)
         {}

你怎么解释这个?您的建议将不胜感激。

2 个答案:

答案 0 :(得分:1)

最后一个<a>的元素<div class="ec_statements">中没有任何文字:

<a href="/Legislation-and-Politics/Legislative-Alerts/Letter-to-Senators-Urging-Them-to-Vote-Yes-
             on-the-Motion-to-Proceed-to-the-Emergency-Supplemental-Appropriations-Act-of-2014-S.2648">
</a>

将此与上面的另一个div进行比较:

<a href="/Legislation-and-Politics/Legislative-Alerts/Letter-to-  
Representatives-Urging-Them-to-Vote-on-the-Highway-Trust-Fund-Bill">
Letter to Representatives Urging Them to Vote on the Highway Trust Fund Bill
</a>

如您所见,第二个示例中的文字位于<a>标记之后和</a>标记之前。在第一个例子中,没有这样的文本。

答案 1 :(得分:0)

您正在调用element.a.get_text()来生成密钥,但最后一个元素的a标记没有文字内容:<a ...></a>