0

I am attempting to open the same desired webpage a selected amount of times (by the user) but I can't seem to get it to work.

#Imports modules
import requests
import time

#Assigning variables to strings
import urllib.request

url = "https://r6tab.com/bd6a3f35-5060-499a-8645-369664aae1d9"

# Open the URL as Browser, not as python urllib
how = input("Enter how many views you want!")


for counter in range (0,how):
     page=urllib.request.Request(url,headers={'User-Agent': 'Mozilla/5.0'})
     infile=urllib.request.urlopen(page).read()
Traceback (most recent call last):
  File "C:/Users/lauchlan/Desktop/sss.py", line 14, in <module>
    for counter in range (0,how):
TypeError: 'str' object cannot be interpreted as an integer
>>> 

Fixed error but views on the webpage arent counting

halfer
  • 19,824
  • 17
  • 99
  • 186
xnx
  • 57
  • 1
  • 9
  • 1
    What is the problem you have? – Pitto Nov 02 '18 at 16:58
  • edited to include error, my bad :( – xnx Nov 02 '18 at 17:04
  • 2
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – Jongware Nov 02 '18 at 17:08
  • All `input()` comes in as `str` and you can't use `range()` with `str` at one end – G. Anderson Nov 02 '18 at 17:09
  • Hello @xnx! I've added an answer, does it work for you? If so kindly remember to upvote and/or choose it as answer :) – Pitto Nov 05 '18 at 10:00
  • 1
    It looks like you know how to accept answers here, @xnx. Although it is not mandatory to accept answers, if someone reminds you about it, remember that they took the time to help you. Some things are just good to do, even if they are not obligations `:-)`. – halfer Aug 03 '19 at 17:05

2 Answers2

1

input returns a string. You need to convert to an integer to use it in range. Try:
how = int(input("Enter how many views you want!"))

Joyal Mathew
  • 614
  • 7
  • 24
  • I got that and got rid of the error but the views on the page arent counting – xnx Nov 02 '18 at 17:11
  • How are you expecting the number of views to go up? Do you have some kind of view counter on the page? – abhi Nov 02 '18 at 17:13
  • yes it counts the page visits on the page, so if i enter 1000 i expect it to go up by 1000 – xnx Nov 02 '18 at 17:17
0

Please try this code, I think it is very straight-forward:

import requests

url = "https://r6tab.com/bd6a3f35-5060-499a-8645-369664aae1d9"
how = int(input("Enter how many views you want: "))
headers={'User-Agent': 'Mozilla/5.0'}

for counter in range (0,how):
     print "Counter value:\n" + str(counter)
     response = requests.get(url)
     print "Response status:\n" + str(response.status_code)
     page = response.text
     print "Response text:\n" + response.text

Keep in mind that your counter could be intelligent enough to stop counting visits from the same IP in a short period of time.

halfer
  • 19,824
  • 17
  • 99
  • 186
Pitto
  • 8,229
  • 3
  • 42
  • 51