python - 可以打印但不能返回 html 表 : "TypeError: ResultSet object is not an iterator"

标签 python flask beautifulsoup mechanize-python

这里是Python新手。 Python 2.7 和 beautifulsoup 3.2.1。

我正在尝试从一个简单的页面中抓取一个表格。我可以轻松地打印它,但无法让它返回到我的 View 功能。

以下作品:

@app.route('/process')
def process():

   queryURL = 'http://example.com'

   br.open(queryURL)

   html = br.response().read()
   soup = BeautifulSoup(html)
   table = soup.find("table")
   print table

   return 'All good'

我也可以成功返回html。但是,当我尝试返回表而不是返回'All good'时,我收到以下错误:

TypeError: ResultSet object is not an iterator

我也尝试过:

br.open(queryURL)

html = br.response().read()
soup = BeautifulSoup(html)
table = soup.find("table")
out = []
for row in table.findAll('tr'):
    colvals = [col.text for col in row.findAll('td')]
    out.append('\t'.join(colvals))

return table

没有成功。有什么建议吗?

最佳答案

您正在尝试返回一个对象,但您实际上并未获取该对象的文本,因此 return table.text应该是你正在寻找的。完整修改代码:

def process():

   queryURL = 'http://example.com'

   br.open(queryURL)

   html = br.response().read()
   soup = BeautifulSoup(html)
   table = soup.find("table")
   return table.text

编辑:

因为我现在知道您需要形成网站的 HTML 代码而不是值,所以您可以执行类似于我所做的示例的操作:

import urllib

url = urllib.urlopen('http://www.xpn.org/events/concert-calendar')
htmldata = url.readlines()
url.close()

for tag in htmldata:
    if '<th' in tag:
        print tag
    if '<tr' in tag:
        print tag
    if '<thead' in tag:
        print tag
    if '<tbody' in tag:
        print tag
    if '<td' in tag:
        print tag

你不能用 BeautifulSoup 做到这一点(至少据我所知),因为 BeautifulSoup 更多的是用于以漂亮的方式解析或打印 HTML。您可以按照我的做法,用一个 for 循环遍历 HTML 代码,如果该行中有一个标签,则打印它。

如果您想将输出存储在列表中以供以后使用,您可以执行以下操作:

htmlCodeList = []
for tag in htmldata:
        if '<th' in tag:
            htmlCodeList.append(tag)
        if '<tr' in tag:
            htmlCodeList.append(tag)
        if '<thead' in tag:
            htmlCodeList.append(tag)
        if '<tbody' in tag:
            htmlCodeList.append(tag)
        if '<td' in tag:
            htmlCodeList.append(tag)

这会将 HTML 行保存在列表的新元素中。所以<td>将是索引 0,下一组标签将是索引 1,等等。

关于python - 可以打印但不能返回 html 表 : "TypeError: ResultSet object is not an iterator",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24701499/

相关文章:

python - 如何使用 python stomp.py 客户端在 activemq 中安排延迟消息

python - 到 Flask 的环回访问 token

python - Flask 表单验证设计

Python 列表多重赋值

python - 如何更改 tqdm 的条形大小

python - 修复 Flask apache 应用程序中的绝对 url

python - 输出是重复的,从第二个开始

python - 如何抓取特定文本

python - 如何循环遍历BS4数据并正确打印div标签

python - 输出视频没有声音