質問

は、今そのファイルへの書き込みに設定したが、私は出力に変数に値をそれをしたいです。いないことを確認する方法。

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()
役に立ちましたか?

解決

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)

他のヒント

GETはすべてがGE_DBの言及を取り除きます。

ドゥA    outputtext = "" 初めに向けます。

に置き換えGE_DB.write(col.string if col.string else '')outputtext += col.string if col.string else ''

<ストライキ>多分このような。

<ストライキ>
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

<ストライキ>

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()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top