900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Python通过urllib访问网页

Python通过urllib访问网页

时间:2020-04-19 22:31:29

相关推荐

Python通过urllib访问网页

通过urllib.request.urlopen()函数就可以访问网页了

>>> import urllib.request

>>> response =urllib.request.urlopen("")

>>> html =response.read()

>>> print(html)

b'<!DOCTYPE html><html itemscope="" itemtype="/WebSite"><head><link rel="stylesheet" media="screen" href="/assets/application-50d340b4770955a3305bd75703b1fbd40448de80151abddce4336475a5959192.css" /><link href=\'/assets/icons/favicon-db66290d1eb239acedfed4e94028fc8697ba119452f28461f3c40d517d006f3d.ico\' rel=\'icon\' type=\'image/x-icon\'>\n<link href=\'/assets/icons/touch-icon-iphone-cef164b4d6d29e1bff2bad9e49abaf143593a07d8a6e584f472b545b9e0c5631.png\' rel=\'apple-touch-icon\'>\n<link href=\'/assets/icons/touch-icon-ipad-7e9822ba1e8fa34ce37262f6746dbc72819d754f805a410dbeb2cedb08a05789.png\' rel=\'apple-touch-icon\' sizes=\'76x76\'>\n<link href=\'/assets/icons/touch-icon-iphone-retina-42b99bfa7f037e7773362a76bf1ee70632541e4054f9003ce6f190dc9070fd8d.png\' rel=\'apple-touch-icon\' sizes=\'120x120\'>\n<link href=\'/assets/icons/touch-icon-ipad-retina-56a8f2519ed554a55e6084d77c1ab7ef3511f8ed5e7877db50a9865621a79290.png\' rel=\'apple-touch-icon\' sizes=\'152x152\'>\n<meta charset="utf-8" /><meta content="IE=edge" http-equiv="X-UA-Compatible" /><meta content="width=device-width, minimum-scale=1, initial-scale=1" name="viewport" /><meta name="csrf-param" content="authenticity_token" />\n<meta name="csrf-token" content="TWquJFnvGbztzfhFmUHFn7fuWGDnd/hNQMqWkGhKDMlFh1bwgIC0f+eUpJ5DgeFbUCcjMOCPmFuBXAVPExHzBQ==" /><title>The domain name is for sale | Undeveloped</title><meta content="The domain name is for sale. Make an offer or buy it now at a set price. Undeveloped keeps you safe." name="description" /><meta content="nofollow" name="robots" /><link href="" rel="canonical" /><link href="" hreflang="en-us" itemprop="url" rel="alternate" /><link href="/nl-nl" hreflang="nl-nl" itemprop="" rel="alternate" /><link href="/es-es" hreflang="es-es" itemprop="" rel="alternate" /><link href="/hi-in" hreflang="hi-in" itemprop="" rel="alternate" /><link href="/de-de" hreflang="de-de" itemprop="" rel="alternate" /><link href="/fr-fr" hreflang="fr-fr" itemprop="" rel="alternate" /><link href="/zh-cn" hreflang="zh-cn" itemprop="" rel="alternate" /><meta content=" - Domain Name For Sale | Undeveloped" property="og:title" /><meta content="product" property="og:type" /><meta content="/" property="og:url" /><meta content="I found a great domain name for sale. Check it out!" property="og:description" /><meta content="Undeveloped" property="og:site_name" /><meta content="4995" property="og:price:amount" /><meta content="USD" property="og:price:currency" /><meta content="summary" name="twitter:card" /><meta content="@undeveloped" name="twitter:site" /><meta content=" - Domain Name For Sale | Undeveloped" name="twitter:title" /><meta content="I found a great domain name for sale. Check it out!" name="twitter:description" /><meta content="@undeveloped" name="twitter:creator" /></head><body><div itemprop="author" itemscope="" itemtype="/Organization" style="display:none!important;"><meta content="Undeveloped" itemprop="name" name="author" /><meta content="/assets/logos/logo-undeveloped-gray-41752f2131b3c81f59add0f28168beb14da3e422562addda9348e3ea2b53ae8e.png" itemprop="logo" /></div><header class="headroom"><nav class="navbar navbar-default navbar-parking"><div class="navbar-header"><div class="col-xs-12"><a target="_blank" class="navbar-brand logo-with-text" href="/?utm_campaign=Parking&amp;utm_medium=parking&amp;utm_source=.com&amp;utm_term="><img class="img-responsive" alt="Undeveloped" src="/assets/logos/logo-undeveloped-gray-safe-d2bbdd8aeb867666a6399c0a26866e77e46deee61af0b5622822ab6f7ed80149.png" /></a></div><div class="col-xs-12 p-t-1 p-x-0 m-x-0 visible-xs"><!-- TrustBox widget - Pop-Up --> <div class="trustpilot-widget" data-locale="en-US" data-template-id="5418015fb0d04a0c9cf721f2" data-businessunit-id="5aa29819243d5a0001f9665a"data-style-height="30px" data-style-width="100%" data-theme="light" data-stars="1,2,3,4,5"><ahref="/review/"target="_blank"></a> </div> <!-- End TrustBox widget -->\n</div></div><div class="pull-right p-y-2 p-x-2 hidden-xs"><!-- TrustBox widget - Pop-Up --> <div class="trustpilot-widget" data-locale="en-US" data-template-id="5418015fb0d04a0c9cf721f2" data-businessunit-id="5aa29819243d5a0001f9665a"data-style-height="30px" data-style-width="100%" data-theme="light" data-stars="1,2,3,4,5"><ahref="/review/"target="_blank"></a> </div> <!-- End TrustBox widget -->\n</div></nav></header><nav class="navbar navbar-default"></nav><div class="full-page"><div itemscope="" itemtype="/Product" style="display:none!important;"><meta content="" itemprop="name" /><meta content=".com" itemprop="category" /><meta content="/" itemprop="url" /><div itemprop="offers" itemscope="" itemtype="/Offer"><meta content="" itemprop="name" /><meta content=".com" itemprop="category" /><meta content="/" itemprop="url" /><meta href="/InStock" itemprop="availability" /><meta content="4995" itemprop="price" /><meta content="USD" itemprop="priceCurrency" /><div itemscope="" itemtype="/Organization"><meta content="Mega Domains" itemprop="name" /><meta content="/domain-seller/mega-domains-c6335228-31a9-4e05-be5e-a8a646cc65fe" itemprop="url" /></div></div></div><script>window.currencyRates = {"ISK":107.569888,"AUD":1.351703,"BGN":1.6757,"BRL":3.769397,"CAD":1.32954,"CHF":0.99022,"CNY":6.575796,"CZK":22.199499,"DKK":6.38507,"GBP":0.75567,"HKD":7.849198,"HRK":6.3255,"HUF":278.910004,"IDR":14173.0,"ILS":3.631606,"INR":68.333994,"JPY":109.904999,"KRW":1117.630005,"MXN":19.860701,"MYR":4.020977,"NOK":8.114299,"NZD":1.456602,"PHP":53.630001,"PLN":3.711201,"RON":3.993102,"RUB":63.108101,"SEK":8.86245,"SGD":1.36251,"THB":32.990002,"TRY":4.618006,"ZAR":13.531697,"EUR":0.856699};</script><input id="domain-price" type="hidden" value="4995" /><input id="domain-currency" type="hidden" value="USD" /><script type="application/json" id="js-react-on-rails-context">{"inMailer":false,"i18nLocale":"en-us","i18nDefaultLocale":"en-us","href":"<a href="/%22,%22location%22:%22/%22,%22scheme%22:%22http%22,%22host%22:%%22,%22port%22:null,%22pathname%22:%22/%22,%22search%22:null,%22httpAcceptLanguage%22:null,%22serverSide%22:false%7D%3C/script%3E/n%3Cscript" designtimeurl="'/" script="" ?,?search?:null,?httpacceptlanguage?:null,?serverside?:false}<="" ?,?scheme?:?http?,?host?:??,?port?:null,?pathname?:?="" ,?location?:?="">\n<script'" designtimesp="14403">\n

........

Python爬取的是以utf-8编码的bytes对象,要还原为带中文的html代码,需要对其进行解码,将它变成Unicode编码

>>> html=html.decode("utf-8")

>>> print(html)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。