2017-07-28 91 views
3

我在BeautifulSoup上有一本书和文档。两个人都说我应该能够链接find/find_all方法并使用下标来从单个页面抓取我想要的东西。这似乎并非如此。考虑下表。在BeautifulSoup中无法链接查找和find_all

<tr> 
<td><span style="display:none;" class="sortkey">Dresser !</span><span class="sorttext">**<a href="/wiki/Louise_Dresser" title="Louise Dresser">Louise Dresser</a>**</span></td> 
<td><span style="display:none;" class="sortkey">Ship !</span><span class="sorttext"><i><a href="/wiki/A_Ship_Comes_In" title="A Ship Comes In">A Ship Comes In</a></i></span></td> 
<td><span style="display:none;" class="sortkey">Pleznik !</span><span class="sorttext">Mrs. Pleznik</span></td> 
</tr> 
<tr> 
<td><span style="display:none;" class="sortkey">Swanson !</span><span class="sorttext"><a href="/wiki/Gloria_Swanson" title="Gloria Swanson">Gloria Swanson</a></span></td> 
<td><i><a href="/wiki/Sadie_Thompson" title="Sadie Thompson">Sadie Thompson</a></i></td> 
<td><span style="display:none;" class="sortkey">Thompson !</span><span class="sorttext">Sadie Thompson</span></td> 
</tr> 
<tr> 
<th scope="row" rowspan="6" style="text-align:center"><a href="/wiki/1928_in_film" title="1928 in film">1928</a>/<a href="/wiki/1929_in_film" title="1929 in film">29</a><br /> 
<small><a href="/wiki/2nd_Academy_Awards" title="2nd Academy Awards">(2nd)</a></small></th> 
<td style="background:#FAEB86"><b><span style="display:none;" class="sortkey">Pickford !</span><span class="sorttext">**<a href="/wiki/Mary_Pickford" title="Mary Pickford">Mary Pickford</a>**</span> <img alt="Award winner" src="//upload.wikimedia.org/wikipedia/commons/f/f9/Double-dagger-14-plain.png" width="9" height="14" data-file-width="9" data-file-height="14" /></b></td> 

对于每一个表行,我需要抓住的第一个元素,那么第一个嵌套的标签内的文本。 Lousie Dresser将成为第一个数据点,接下来是Gloria Swanson,然后是Mary Pickford。

我想以下会让我在那里,但我错了,6小时后,我花了。

def getActresses(URL): 
    try: 
     html = urlopen(URL) 
    except HTTPError: 
     print("Page not found.") 
     return None 
    try: 
     bsObj = BeautifulSoup(html, "lxml") 
     soup = bsObj.find("table", {"class":"wikitable sortable"}) 
    except AttributeError: 
     print("Error creating/navigating soup object") 
    data = soup.find_all("tr").find_all("td").find("a").get_text() 
    print(data) 


getActresses("https://en.wikipedia.org/wiki/Academy_Award_for_Best_Actress") 

这不是我试过的唯一代码。我试着循环遍历行,然后表数据单元格,然后访问标签。我试过要求一个标签,然后将它们重新排列出来,只是被告知我不能拥有我想要的文本。尝试连锁操作时遇到的最常见错误(如上所述)为AttributeError: 'ResultSet' object has no attribute 'find'.即使在复制书籍示例时,下标也不起作用(请参阅图?!)。另外,我已经让流程放弃了自己,我不知道这是可能的。

关于正在发生的事情以及为什么应该如此简单的事情似乎是这样的事件的想法将非常感激。

回答

5
import requests 
from bs4 import BeautifulSoup 

def getActresses(URL): 
    res = requests.get(URL) 

    try: 
     soup = BeautifulSoup(res.content, "lxml") 
     table = soup.find("table", {"class":"wikitable sortable"}) 
    except AttributeError: 
     print("Error creating/navigating soup object") 

    tr = table.find_all("tr") 

    for _tr in tr: 
     td = _tr.find_all("td") 
     for _td in td: 
      a = _td.find_all("a") 
      for _a in a: 
       print(_a.text.encode("utf-8")) 

getActresses("https://en.wikipedia.org/wiki/Academy_Award_for_Best_Actress") 

使用的text代替get_text()和抱歉,我用requests模块演示

find_all方法总是返回一个列表,以便您通过它

对不起,我是个新的有循环stackoverflow,我不知道如何写答案。无论如何,我相信代码将清除你的疑惑

+0

绝对在正确的轨道上,不需要道歉。我不能为所有人说话,但是我的经验是,大多数人都很友善。你的回复让我关上了。输出是一个名称列表(这是很好的),但是它们的顺序是错误的,并且在答案集中包含了不希望出现的名称,所以我的下一个任务将对此进行排序,但是即使到那个点也会有没有你的帮助是不可能的。非常感谢! :) – Ryan