Hi,
I've just started learning Python and for my first script I wanted to make something that would make life easier and that I would actually use, so starting small I've began working on a Torrent Search script that uses the ISOHunt API. However I've ran into a problem in the unfinished method that cleans the results from the search:

def sanitise(string):
    a = string.split('{"title":')           # split page into parts, a is list
    for i in range(len(a)):
        a[i].lower()                                            # lower every char on page
        index = a[i].find('"enclosure_url":')
        b = a[i].replace(a[:index], "")
        print a[i]

Here's an example search result:

<b>Inception</b>.FRENCH.DVDRip.XViD-DVDFR",
"link":"http:\/\/isohunt.com\/torrent_details\/255340617\/inception?tab=summary",
"guid":"255340617","enclosure_url":
"http://ca.isohunt.com........."

My script only needs 3 pieces of information, so basically what I'm trying to do is cut out the bit of string that is in front of "enclosure_url" but from my code above, I keep getting the error:
"TypeError: expected a character buffer object".

Could anyone help me fix this?

Recommended Answers

All 2 Replies

Why not just split at "enclosure_url" rather than "title" and keep only the half you want?

I keep getting the error:
"TypeError: expected a character buffer object"

We can't help without the complete error message; which tells where the error is in the program among other things.

    a[i].lower() 

Take a look at this page http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/strings3.html beginning at the paragraph that starts with "Strings are immutable, so no string method can change the original string"

Also,

    index = a[i].find('"enclosure_url":')
    b = a[i].replace(a[:index], "")

what happens if it is not found?

##  NOT Tested
def sanitise(str_in):
    to_find = '"enclosure_url":'
    print "-"*50
    str_in = str_in.lower()
    if to_find in str_in:
        result_list = str_in.split(to_find)
        print "Found", result_list
        return result_list[0]
    else:
        print "NOT found in", str_in
    return []
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.