如何使结果成为变量?

2024-10-02 04:18:45 发布

您现在位置:Python中文网/ 问答频道 /正文

现在它的设置是写入一个文件,但我希望它将值输出到一个变量。不知道怎么做。在

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()

Tags: inimportrefordbstringcolurllib2
3条回答
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)

可能是这样。

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

别再提葛优数据库了。在

做一个 outputtext = "" 开始的时候。在

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

相关问题 更多 >

    热门问题