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.

Aquarthur
  • 609
  • 5
  • 20
Angus
  • 447
  • 1
  • 4
  • 10
  • This is a duplicate post. refer : https://stackoverflow.com/questions/35168508/raw-input-is-not-defined please close this post – roshan ok Nov 05 '19 at 15:17

2 Answers2

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':.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • Ah I thought in Python input was only for integers, not string entries. Thanks. – Angus Nov 05 '19 at 15:12
  • @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 – steviestickman Nov 05 '19 at 15:14
  • 1
    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. – Matthias Nov 05 '19 at 15:19
  • This is a duplicate post. refer : stackoverflow.com/questions/35168508/raw-input-is-not-defined please close this pos – roshan ok Nov 05 '19 at 15:24
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
Govinda Malavipathirana
  • 1,095
  • 2
  • 11
  • 29