我有一个URL http://apache.domain.com/get.php?id=1001使用Python获取远程URL
我是新来的蟒蛇想蟒蛇连接到URL,如果返回的页面是空的,则:
print("Content Empty")
否则:
print("Has content")
关于我如何做到这一点的任何建议?
感谢
我有一个URL http://apache.domain.com/get.php?id=1001使用Python获取远程URL
我是新来的蟒蛇想蟒蛇连接到URL,如果返回的页面是空的,则:
print("Content Empty")
否则:
print("Has content")
关于我如何做到这一点的任何建议?
感谢
你可以使用urllib.request
stdlib module获取的网址:
#!/usr/bin/env python3
from urllib.request import urlopen
try:
with urlopen("http://apache.domain.com/get.php?id=1001") as response:
print("Has content" if response.read(1) else "Content Empty")
except OSError as e:
print("error happened: {}".format(e))
真棒。完美的作品。谢谢 – John
我建议使用Python Requests:
import requests
response = requests.get("http://apache.domain.com/get.php?id=1001")
print response.text
然后,您可以采取取决于什么response.text
包含了必要的行动。
'response.text'读取所有(可能无限制)的内容并尝试将其解码为Unicode。如果您只需要查明页面是否为空,那么这是不必要的,并且可能是有害的。 – jfs
你实际上是试图找到如果页面是空的或实际存在? –