没有请求模块错误

时间:2017-07-08 20:24:48

标签: python python-3.x python-import importerror

我正在尝试使用请求模块编写Web解析器脚本。这是我目前的代码:

import requests
import subprocess
import json
import sys
import threading
import time
from Queue import Queue

numberOfViewers = int(sys.argv[1])
builderThreads = int(sys.argv[2])
startTime = time.time()
numberOfSockets = 0
concurrent = 25
urls = []
urlsUsed = []

def getURL(): # Get tokens
  output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"],
  stdout=subprocess.PIPE).communicate()[0]
  return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter

def build(): # Builds a set of tokens, aka viewers
  global numberOfSockets
  global numberOfViewers
  while True:
    if numberOfSockets < numberOfViewers:
      numberOfSockets += 1
      print ("Building viewers " + str(numberOfSockets) + "/" + str(numberOfViewers))
      urls.append(getURL())

def view(): # Opens connections to send views
  global numberOfSockets
  while True:
    url=q.get()
    requests.head(url)
    if (url in urlsUsed):
      urls.remove(url)
      urlsUsed.remove(url)
      numberOfSockets -= 1
    else:
      urlsUsed.append(url)
      q.task_done()

      if __name__ == '__main__':
        for i in range(0, builderThreads):
          threading.Thread(target = build).start()

          while True:
            while (numberOfViewers != numberOfSockets): # Wait until sockets are built
              time.sleep(1)

              q=Queue(concurrent*2)
              for i in range(concurrent):
                try:
                  t=threading.Thread(target=view)
                  t.daemon=True
                  t.start()
                except:
                  print ('thread error')
                  try:
                    for url in urls:
                      print (url)
                      q.put(url.strip())
                      q.join()
                  except KeyboardInterrupt:
                    sys.exit(1)

但是当我运行代码时,它说:

Traceback (most recent call last):
  File "C:\Users\flamelier\Desktop\Twitch.py", line 1, in <module>
    import requests
ImportError: No module named 'requests'

为什么我收到此错误?我该如何安装这个模块? 此错误是否会继续为所有脚本重复? 如何在将来防止此类类似错误?

3 个答案:

答案 0 :(得分:1)

请求是第三方模块。您应该首先使用PIP或easy_install将其安装到Python。

答案 1 :(得分:0)

您必须运行pip3 install requests,因为requests默认情况下并不附带Python,因为它是第三方库。

答案 2 :(得分:0)

即使你有pip3安装的请求,显示的代码也不会做任何事情。

    if __name__ == "__main__"

测试及其后面的所有内容都是view函数中else块的一部分。返回此行和后面的块到左边距。