1

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
  • Try print(item) and you will see its not a string, but a tuple. You can only add strings together. Commented Nov 18, 2014 at 17:08

1 Answer 1

1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.