如何将其结果设为变量?

问题描述 投票:0回答:4

现在它设置为写入文件,但我希望它将值输出到变量。不知道怎么办。

from BeautifulSoup import BeautifulSoup
import sys, re, urllib2
import codecs


woof1 = urllib2.urlopen('someurl').read()
woof_1 = BeautifulSoup(woof1)
woof2 = urllib2.urlopen('someurl').read()
woof_2 = BeautifulSoup(woof2)

GE_DB = open('GE_DB.txt', 'a')

for row in woof_1.findAll("tr", { "class" : "row_b" }):
  for col in row.findAll(re.compile('td')):
    GE_DB.write(col.string if col.string else '')
GE_DB.write("   ")
GE_DB.write("\n")
GE_DB.close()
for row in woof_2.findAll("tr", { "class" : "row_b" }):
  for col in row.findAll(re.compile('td')):
    GE_DB.write(col.string if col.string else '')
GE_DB.write("\n")
GE_DB.close()
python variables beautifulsoup
4个回答
-1
投票
values = []
for row in woof_1.findAll("tr", { "class" : "row_b" }):
  for col in row.findAll(re.compile('td')):
    if col.string:
      values.append(col.string)
result = ''.join(values)

-1
投票

删除所有提及 GE_DB 的内容。

做一个

outputtext = ""
走向开始。

GE_DB.write(col.string if col.string else '')
替换为
outputtext += col.string if col.string else ''


-1
投票

也许是这样的。

gedb = "";
for row in woof_1.findAll("tr", { "class" : "row_b" }):
  for col in row.findAll(re.compile('td')):
    if col.string:
      gedb += col.string


-2
投票
import cStringIO as StringIO   # or import StringIO if on a fringe platform
buf = StringIO.StringIO()
for row in woof_1.findAll("tr", { "class" : "row_b" }):
  for col in row.findAll(re.compile('td')):
    buf.write(col.string if col.string else '')

result = buf.getvalue()
© www.soinside.com 2019 - 2024. All rights reserved.