Python程序要以用户选择的次数查看同一网页

时间:2018-11-02 16:56:44

标签: python python-3.x

我正在尝试按用户选择的次数打开相同的所需网页,但似乎无法正常工作。

#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
>>> 

已修复错误,但网页上的观看次数未计算

2 个答案:

答案 0 :(得分:1)

input返回一个字符串。您需要转换为整数才能在range中使用它。试试:
how = int(input("Enter how many views you want!"))

答案 1 :(得分:0)

请尝试使用此代码,我认为这非常简单(如果您有任何疑问,请告诉我):

HTTP/1.1 200 OK
Date: Fri, 02 Nov 2018 21:50:28 GMT
Server: Apache/2.4.34 (Amazon) mod_wsgi/3.5 Python/3.6.5
Expires: Fri, 02 Nov 2018 21:50:29 GMT
Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Vary: Cookie,Origin
X-Frame-Options: SAMEORIGIN
Set-Cookie: csrftoken=8Aw8toVirE5qLSt79Nhh5tDem59qLChCrZ3i7zKkLo2NzS1UZ37SVDpjl; expires=Fri, 01-Nov-2019 21:50:29 GMT; Max-Age=31449600; Path=/
Content-Length: 3876
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8

请记住,您的计数器可能足够聪明,可以在短时间内停止对来自同一IP的访问进行计数。

快乐黑客:)