Python-调用URL时的页面源代码
我正在寻找一个真正简单的代码来调用url和打印html源代码。这就是我正在使用的。我正在学习一门有代码的在线课程Python-调用URL时的页面源代码,python,python-3.x,Python,Python 3.x,我正在寻找一个真正简单的代码来调用url和打印html源代码。这就是我正在使用的。我正在学习一门有代码的在线课程 def get_page(url): try: import urllib return urllib.open(url).read() except: return "" print(get_page('https://www.yahoo.com/')) 不打印任何内容,但也没有错误。或者浏览这些我尝试过的论坛 from urllib.request i
def get_page(url):
try:
import urllib
return urllib.open(url).read()
except:
return ""
print(get_page('https://www.yahoo.com/'))
不打印任何内容,但也没有错误。或者浏览这些我尝试过的论坛
from urllib.request import urlopen
print (urlopen('https://xkcd.com/353/'))
当我这样做的时候,我会
<http.client.HTTPResponse object at 0x000001E947559710>
假设使用了UTF-8编码
from urllib import request
def get_src_code(url):
r = request.urlopen("url")
byte_code = r.read()
src_code = bytecode.decode()
return src_code
它在
块处打印空字符串,但
块除外。您的代码正在生成错误,因为在urllib
模块中没有名为open
的属性。您无法看到错误,因为您正在使用try-except
块,该块在每个错误时都返回空字符串。在代码中,您可以看到如下错误:
def get_page(url):
try:
import urllib
return urllib.open(url).read()
except Exception as e:
return e.args[0]
def get_page(url):
try:
from urllib.request import urlopen
return urlopen(url).read().decode('utf-8')
except Exception as e:
return e.args[0]
要获得预期的输出,请执行以下操作:
def get_page(url):
try:
import urllib
return urllib.open(url).read()
except Exception as e:
return e.args[0]
def get_page(url):
try:
from urllib.request import urlopen
return urlopen(url).read().decode('utf-8')
except Exception as e:
return e.args[0]
看看这个