Python 美丽的汤元素访问

Python 美丽的汤元素访问,python,beautifulsoup,Python,Beautifulsoup,我正在尝试使用BeautifulSoup从网页中提取信息。我的代码在这里: from bs4 import BeautifulSoup import urllib2 opener = urllib2.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0')] infile = opener.open('http://en.wikipedia.org/wiki/American_films_of_1971') page =

我正在尝试使用BeautifulSoup从网页中提取信息。我的代码在这里:

from bs4 import BeautifulSoup
import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
infile = opener.open('http://en.wikipedia.org/wiki/American_films_of_1971')
page = infile.read()
soup = BeautifulSoup(page)
soup.prettify().encode('utf8')
print (soup.find_all("table", "wikitable"))
输出

[<table class="wikitable">
<tr>
<th style="width:25%;">Title</th>
<th style="width:20%;">Director</th>
<th style="width:30%;">Cast</th>
<th style="width:10%;">Genre/Note</th>
<th style="width:3%;">
<p><br/></p>
</th>
</tr>
<tr>
<td><i><a class="mw-redirect" href="/wiki/$" title="$">$</a> aka Dollars</i></td>
<td><a href="/wiki/Richard_Brooks" title="Richard Brooks">Richard Brooks</a></td>
<td><a href="/wiki/Warren_Beatty" title="Warren Beatty">Warren Beatty</a>, <a href="/wiki/Goldie_Hawn" title="Goldie Hawn">Goldie Hawn</a></td>
<td><a href="/wiki/Comedy" title="Comedy">Comedy</a>, <a href="/wiki/Crime" title="Crime">Crime</a></td>
<td></td>
</tr>
<tr>
<td><i><a href="/wiki/200_Motels" title="200 Motels">200 Motels</a></i></td>
<td><a href="/wiki/Tony_Palmer" title="Tony Palmer">Tony Palmer</a>, Charles Swenson</td>
<td><a href="/wiki/Frank_Zappa" title="Frank Zappa">Frank Zappa</a>, <a href="/wiki/Ringo_Starr" title="Ringo Starr">Ringo Starr</a>, <a href="/wiki/Theodore_Bikel" title="Theodore Bikel">Theodore Bikel</a></td>
<td><a href="/wiki/Comedy" title="Comedy">Comedy</a>, <a href="/wiki/Musical_film" title="Musical film">Musical</a></td>
<td></td>
</tr>
</table>]
我不确定在得到我想要的文档部分后如何查看子标签


我想知道BeautifulSoup是否是正确的工具,或者我是否应该看看其他东西

.find_all()
列表中的每个结果都是另一个元素对象,因此您可以对这些对象进行进一步搜索:

for table in soup.find_all("table", "wikitable"):
    for row in table.find_all('tr'):
        cells = []
        for cell in row.find_all('td'):
            cells.append(cell.get_text())
        print(' | '.join(cells))
这给了我:

$ aka Dollars | Richard Brooks | Warren Beatty, Goldie Hawn | Comedy, Crime | 
200 Motels | Tony Palmer, Charles Swenson | Frank Zappa, Ringo Starr, Theodore Bikel | Comedy, Musical | 

.find_all()
列表中的每个结果都是另一个元素对象,因此您可以对以下内容进行进一步搜索:

for table in soup.find_all("table", "wikitable"):
    for row in table.find_all('tr'):
        cells = []
        for cell in row.find_all('td'):
            cells.append(cell.get_text())
        print(' | '.join(cells))
这给了我:

$ aka Dollars | Richard Brooks | Warren Beatty, Goldie Hawn | Comedy, Crime | 
200 Motels | Tony Palmer, Charles Swenson | Frank Zappa, Ringo Starr, Theodore Bikel | Comedy, Musical | 

不要使用外部易失性服务来提供代码。那么,解决你的问题……具体的问题是什么?到底是什么不起作用?您可以使用Beauifulsoup、lxml或其他任何东西解析HTML…问题是?除此之外:soup.find_all(“table”、“wikitable”))对于搜索class=“wikitable”没有任何意义。请阅读汤的文档。查找所有(“表格”、“维基表格”)显然是错误的。请查阅您的文档并查看代码的外观:不要使用外部易失性服务来提供代码。那么,解决你的问题……具体的问题是什么?到底是什么不起作用?您可以使用Beauifulsoup、lxml或其他任何东西解析HTML…问题是?除此之外:soup.find_all(“table”、“wikitable”))对于搜索class=“wikitable”没有任何意义。请阅读汤的文档。查找所有(“表格”、“维基表格”)显然是错误的。请查阅您的文档,了解代码的外观: