The import urllib. Request
The import urllib. Error
Def the main () :
Askurl (" https://movie.douban.com/top250? Start=0 ")
Def askurl (url) :
The head={
"The 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. MetaSr X 1.0 "
}
The req=urllib. Request. The request (url, headers=head)
Try:
Res=urllib. Request. Urlopen (the req)
HTML=res. The read (). The decode (" utf-8 ")
Print (HTML)
Except urllib. Error. URLError as e:
If hasattr (e, "code") :
Print (ode) of e.c. with our fabrication:
If hasattr (e, "" reason") :
Print (" e.r eason)
# return HTML
If __name__=="__main () __" :
The main ()
Above is the code of network training teachers, according to play is not to any information, also is not an error; Their modified into a function call can also come out, please use wrong thank you where is the great god guiding function
Url="https://movie.douban.com/top250? Start=0 "
The head={
"The 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. MetaSr X 1.0 "
}
The req=urllib. Request. The request (url, headers=head)
Try:
Res=urllib. Request. Urlopen (the req)
HTML=res. The read (). The decode (" utf-8 ")
Print (HTML)
Except urllib. Error. URLError as e:
If hasattr (e, "code") :
Print (ode) of e.c. with our fabrication:
If hasattr (e, "" reason") :
Print (" e.r eason)