0

I am trying to write a basic Twitter scraper in Python and while I have it so that it can scrape for hard coded terms, I'm trying to set it to take the search term from user input.

While my if/else statement accepts input when asked, it then fails to run stating that rawinput is not defined. The rawinput is within the if statement I've included my code below

I should mention I'm fairly new to Python.

I've tried removing the rawinput from the if/else and kept it separate but the same issue happens.

userinp = input("Select search type.  1 = tweets.  2 = people")

if userinp == 1:
        entry = rawinput
        query = u'q='
elif userinp == 2:
        entry = rawinput
        query = u'f=users&vertical=default&q='

searchurl = baseurl + query + entry

The expected result is that the user selects option 1 or 2 then is asked to enter their search term.

Results are:

Select search type.  1 = tweets.  2 = people1
Traceback (most recent call last):
  File "Scrape.py", line 22, in <module>
    userentry = rawinput('enter search term')
NameError: name 'rawinput' is not defined

Thanks in advance for any help given.

1

2 Answers 2

3

Use input() instead and don't forget the parentheses!

Additionally, input() converts your input to a string, so your if condition will never be met if it uses integers. Consider replacing with if userinp == '1': and elif userinp == '2':.

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

4 Comments

Ah I thought in Python input was only for integers, not string entries. Thanks.
@Angus in python 2 input() calls eval(raw_input()). in python3 raw_input is renamed to input and python2's input is removed for security reasons
And of course the if statements won't work as planned because userinp will never be the number 1 or 2. We will get the string "1" or "2" instead.
This is a duplicate post. refer : stackoverflow.com/questions/35168508/raw-input-is-not-defined please close this pos
1

It should be raw_input() not rawinput. so your code should be like this...

userinp = input("Select search type.  1 = tweets.  2 = people")

if userinp == 1:
        entry = raw_input()
        query = u'q='
elif userinp == 2:
        entry = raw_input()
        query = u'f=users&vertical=default&q='

searchurl = baseurl + query + entry

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.