代码之家  ›  专栏  ›  技术社区  ›  xnx

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

  •  0
  • xnx  · 技术社区  · 6 年前

    我正试图打开相同的理想网页一个选定的次数(由用户),但我似乎无法得到它的工作。

    #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 回复  |  直到 5 年前
        1
  •  1
  •   Joyal Mathew    6 年前

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

        2
  •  0
  •   halfer    5 年前

    请尝试这个代码,我认为它非常直截了当:

    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
    

    请记住,您的计数器可能足够智能,可以在短时间内停止计算来自同一IP的访问次数。