Convert tuple to string after parsing html file

639 views Asked by At

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.

1

There are 1 answers

0
vikramls On

The issue is that each item in the list called urls is a tuple. A tuple is a container for other items and is also immutable. When you do item + "\n", you are asking the interpreter to concatenate a tuple and a string which is not possible.

What you want to do instead is inspect the tuple and select one of the fields in each item to write to the outfile:

with open("1.txt", "w+") as outfile:
    for item in urls:
        outfile.write(str(item[1]) + "\n") 

Here the 1st field of the tuple item is first converted to string (if it happens to be something else) and then concatenated with "\n". If you wanted to write the tuple as is, you would write this:

outfile.write(str(item) + "\n")