import urllib.request
import urllib.error
def main():
askurl("https://movie.douban.com/top250?start=0")
def askurl(url):
head={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0"
}
req=urllib.request.Request(url,headers=head)
try:
res=urllib.request.urlopen(req)
html =res.read().decode("utf-8")
print(html)
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
# return html
if __name__=="__main()__":
main()
以上是網路培訓老師的代碼,照著打就是出不來任何資訊,也不報錯;自己修改成非函式呼叫還能出的來,請大神們指導函式哪里用錯了謝謝
url="https://movie.douban.com/top250?start=0"
head = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0"
}
req = urllib.request.Request(url, headers=head)
try:
res = urllib.request.urlopen(req)
html = res.read().decode("utf-8")
print(html)
except urllib.error.URLError as e:
if hasattr(e, "code"):
print(e.code)
if hasattr(e, "reason"):
print(e.reason)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/236213.html
上一篇:openmv超聲波測距
