0

I am trying to write a small web request program to get some of the popular Instagram users (this is python 3).

final_list = [];
for idx in range(1, 5, 1):
    url = "http://zymanga.com/millionplus/" + str(idx) + "f"; # idx is for page number
    print(url);
    headers = {...omitted header details...};
    req = urllib.request.Request(url, None, headers);
    with urllib.request.urlopen(req) as f:
        str = f.read().decode('utf-8');
    initial_list = re.findall(r'target=\"_blank\">([^<]+?)</a>,', str);
    for item in initial_list:
        final_list.append(item);

The first iteration works well (and I am able to get the users on the first page), however on the second iteration, I am encounting the following error:

Traceback (most recent call last):
  File ".\web_search.py", line 8, in <module>
    url = "http://zymanga.com/millionplus/" + str(idx) + "f";
TypeError: 'str' object is not callable

Could you please let me know what might caused the problem, tried to debug but couldn't resolved it. Thanks!

1
  • 1
    try url = "http://zymanga.com/millionplus/".join(idx) + "f"; Commented Jun 29, 2017 at 15:39

3 Answers 3

6

You've redefined str within your loop so that it refers to the variable read from the response. Choose a different name.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out!
3
str = f.read().decode('utf-8')

str is the contents of the file you read on the last pass through the loop. You are trying to call it like a function with str(idx) but it is not one.

Don't use the names of built-in functions for your own variables.

1 Comment

Thanks for pointing out!
1

You are using str as a variable at str = f.read().decode('utf-8');. In the next loop, the str(idx) is no longer the class str but the value from f.read().decode('utf-8').

Never use the class types as variable names. Just to illustrate the mistake:

>>> str
<class 'str'>
>>> str(1)
'1'
>>> str = 'some string'
>>> str
'some string'
>>> str(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

1 Comment

Thanks for pointing out!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.