The One Electronic
The One Electronic

Reputation: 129

Python: Get list of URLs from tk.Text and process them

I want to get URLs written as a list in a tk.Text widget and process them systematically one after the other with urllib2.

I’ve written the code below but it doesn’t function. I tried to understand the problem and it seems that at least one problem is that no matter how many lines of text are in the tk.Text widget, the tk.Text.get() method retrieves them as a single element. If I write 3 lines of text in the widget and then call UrlBatchList.count(Urls), I can see that the list has only one element.

def GetBatch(self):
        UrlBatchList = []
        Urls = self.BatchEntryText.get(index1='1.0', index2='end')
        UrlBatchList.append(Urls)

        for Urls in UrlBatchList:
                self.url_target = (Urls)
                self.request = urllib2.Request(self.url_target)
                self.req = urllib2.urlopen(self.request)

How can I retrieve the various lines of text as single elements to be added to the list? How can I do the same with elements (urls) separated by comma (,)?

Upvotes: 1

Views: 109

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386285

You are correct: when you call get, it returns a single string. Since you say the urls are one per line, just split the data on newlines before looping over them.

Urls = self.BatchEntryText.get(index1='1.0', index2='end-1c').split('\n')

Also, you should use 'end-1c' rather than 'end'. That is because tkinter always adds a newline after the last character, which you don't normally want to retrieve.

Finally, use extend rather than append when you want to append each element of a second list. If you use append you are adding the whole list as a single one to the original list.

UrlBatchList.extend(Urls)

Upvotes: 1

Related Questions