Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-检查文件/网页是否存在_Python - Fatal编程技术网

Python-检查文件/网页是否存在

Python-检查文件/网页是否存在,python,Python,我想使用Python根据响应代码检查文件/网页是否存在,并采取相应的行动。但是,我需要使用HTTPS并提供用户名和密码凭据。我无法通过curl运行它。curl不喜欢HTTPS,但通过使用wget和-spider以及-user和-password获得了成功。我想我可以尝试通过os.system将wget合并到脚本中,但它会打印出大量的输出,这将非常难以解析,如果URI不存在,我想会被困在等待响应中 我在网上看过urllib2,看到人们做了一些事情,但我不确定这是否符合我的情况,解决方案总是非常复杂

我想使用Python根据响应代码检查文件/网页是否存在,并采取相应的行动。但是,我需要使用HTTPS并提供用户名和密码凭据。我无法通过curl运行它。curl不喜欢HTTPS,但通过使用wget和-spider以及-user和-password获得了成功。我想我可以尝试通过os.system将wget合并到脚本中,但它会打印出大量的输出,这将非常难以解析,如果URI不存在,我想会被困在等待响应中

我在网上看过urllib2,看到人们做了一些事情,但我不确定这是否符合我的情况,解决方案总是非常复杂,例如。无论如何,如果我能得到一些指导,告诉我最简单的方法是使用python,我将不胜感激


编辑:如果URI存在或不存在,那么使用os.system方法并为wget提供-q似乎会返回一个不同的数字,因此这给了我一些目前需要处理的问题

urllib2是打开任何网页的方式

urllib2.urlopen('http://google.com')
为了增加功能,您需要一个带有处理程序的开场白。我想你只需要https,因为你几乎没有提取任何信息

opener = urllib2.build_opener(
    urllib2.HTTPSHandler())
opener.open('https://google.com')
添加数据后,它将自动成为POST请求,或者我认为:

opener.open('https://google.com',data="username=bla&password=da")
您将接收的对象将具有代码属性

这是它的基本要点,添加尽可能多的操纵者,我相信他们不会伤害你。
来源:

urllib2是打开任何网页的方式

urllib2.urlopen('http://google.com')
为了增加功能,您需要一个带有处理程序的开场白。我想你只需要https,因为你几乎没有提取任何信息

opener = urllib2.build_opener(
    urllib2.HTTPSHandler())
opener.open('https://google.com')
添加数据后,它将自动成为POST请求,或者我认为:

opener.open('https://google.com',data="username=bla&password=da")
您将接收的对象将具有代码属性

这是它的基本要点,添加尽可能多的操纵者,我相信他们不会伤害你。 来源:

您应该使用urllib2检查:

import urllib2, getpass
url = raw_input('Enter the url to search: ')
username = raw_input('Enter your username: ')
password = getpass.getpass('Enter your password: ')
if not url.startswith('http://') or not url.startswith('https://'):
        url = 'http://'+url

def check(url):
        try:
                urllib2.urlopen(url)
                return True
        except urllib2.HTTPError:
                return False

if check(url):
        print 'The webpage exists!'
else:
        print 'The webpage does not exist!'

opener = urllib2.build_opener(
urllib2.HTTPSHandler())
opener.open(url,data="username=%s&password=%s" %(username, password))
其运行方式如下:

bash-3.2$ python url.py
Enter the url to search: gmail.com
Enter your username: aj8uppal
Enter your password: 
The webpage exists!
您应该使用urllib2检查:

import urllib2, getpass
url = raw_input('Enter the url to search: ')
username = raw_input('Enter your username: ')
password = getpass.getpass('Enter your password: ')
if not url.startswith('http://') or not url.startswith('https://'):
        url = 'http://'+url

def check(url):
        try:
                urllib2.urlopen(url)
                return True
        except urllib2.HTTPError:
                return False

if check(url):
        print 'The webpage exists!'
else:
        print 'The webpage does not exist!'

opener = urllib2.build_opener(
urllib2.HTTPSHandler())
opener.open(url,data="username=%s&password=%s" %(username, password))
其运行方式如下:

bash-3.2$ python url.py
Enter the url to search: gmail.com
Enter your username: aj8uppal
Enter your password: 
The webpage exists!
您可以使用python发出HEAD请求

如果请求因ConnectionError失败,则该网站不存在。如果您只想检查某个页面是否存在,您将得到一个成功的响应,但状态代码将为404

请求有一个非常好的界面,所以我建议您检查一下。您可能会非常喜欢它,因为它非常直观和强大,同时又很轻

您可以使用python发出HEAD请求

如果请求因ConnectionError失败,则该网站不存在。如果您只想检查某个页面是否存在,您将得到一个成功的响应,但状态代码将为404


请求有一个非常好的界面,所以我建议您检查一下。您可能会非常喜欢它,因为它非常直观和强大,同时又很轻

也提供用户名和密码证书也提供用户名和密码证书是通过或通过一些自定义登录方案提供的用户名和密码,其中凭证将包含在帖子中?仅需基本身份验证,与curl和wget提供用户名和密码的方式相同。用户名和密码是通过,还是通过一些自定义登录方案提供的,其中凭证将包含在帖子中?只是基本身份验证,与curl和wget提供用户名和密码的方式相同。不确定这是否可行,因为如果您不能提供用户名和密码,web服务器将返回401未经授权的密码。我编辑了代码以获取用户和密码。但是,传递这些数据的确切方式取决于服务器。感谢您提到这个伟大的库。我已经使用urllib2一两年了,但我尽可能快地切换到:“不确定这是否可行,因为如果您无法提供用户名和密码,web服务器将返回401未经授权的密码。我编辑了代码以获取用户和密码。但是,传递这些数据的确切方式取决于服务器。感谢您提到这个伟大的库。我已经在urllib2上工作了一两年,但我尽可能快地切换到了:'