I need to save parsing results in a text file.
import urllib
from bs4 import BeautifulSoup
import urlparse
path = 'A html file saved on desktop'
f = open(path,"r")
if f.mode == 'r':
contents = f.read()
soup = BeautifulSoup(contents)
search = soup.findAll('div',attrs={'class':'mf_oH mf_nobr mf_pRel'})
searchtext = str(search)
soup1 = BeautifulSoup(searchtext)
urls = []
for tag in soup1.findAll('a', href = True):
raw_url = tag['href'][:-7]
url = urlparse.urlparse(raw_url)
urls.append(url)
print url.path
with open("1.txt", "w+") as outfile:
for item in urls:
outfile.write(item + "\n")
However, I get this: Traceback (most recent call last): File "c.py", line 26, in outfile.write(item + "\n") TypeError: can only concatenate tuple (not "str") to tuple.
How can I convert tuple to a string and save it in a text file? Thanks.