从文件读取时request.get无法正常工作

时间:2018-08-01 21:01:10

标签: python-3.x

我正在尝试从文本文件中的网站列表中检查网站是否存在,但是当我读取url时request.get(url)返回空。但是,当我手动输入诸如request.get(“ http://www.google.com”)之类的URL时,它返回200。我如何才能使request.get()与我的url变量一起正常工作?

import requests

filepath = 'url.txt'  
with open(filepath) as fp:  
   for url in fp:
      print(url) #Prints the first line in url.txt which is http://www.google.com
      try:
        request = requests.get(url) #This is returning empty
        print(request)
        if request.status_code == 200:
            print('Web site exists')
        else:
            print("Website returned response code: {code}".format(code=request.status_code))
      except requests.exceptions.ConnectionError:
          print('Web site does not exist') 

对不起,如果我没有正确使用行话,我在编程方面还是很新的

0 个答案:

没有答案