Here is my code.
If I use forloop to write, it got error:
Traceback (most recent call last):
File "m.py", line 20, in <module>
f.write(tag.string)
TypeError: argument 1 must be string or read-only character buffer, not None
HOWEVER, if I don't use for loop, instead
write as : f.write(tag[1].string)
it is ok, WHY??
from BeautifulSoup import BeautifulSoup
import re
import os
import os.path
import urllib2
outfile='out.txt'
file = urllib2.urlopen("http://www.tripadvisor.com/ShowUserReviews-g294217-d305813-r49043831-Langham_Place_Hong_Kong-Hong_Kong_Hong_Kong_Region.html#REVIEWS")
soup = BeautifulSoup(file)
tag=soup.findAll(id=re.compile("^review"))
if os.path.isfile(outfile):
os.remove(outfile)
f=open(outfile, 'a')
for i in range(0,5):
f.write(tag[i].string)