温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python 爬虫 urllib模块:get方式

发布时间:2020-07-07 14:04:20 来源:网络 阅读:367 作者:虎皮喵的喵 栏目:编程语言

本程序以爬取 百度 首页为例

格式:

  导入urllib.request

  打开爬取的网页: response = urllib.request.urlopen('网址')

  读取网页代码: html = response.read()

  打印:

      1.不decode 

      print(html) #爬取的网页代码会不分行,没有空格显示,很难看

      2.decode

      print(html.decode()) #爬取的网页代码会分行,像写规范的代码一样,看起来很舒服

  查询请求结果:

      a. response.status # 返回 200:请求成功  404:网页找不到,请求失败

      b. response.getcode() # 返回 200:请求成功  404:网页找不到,请求失败


1.不decode的程序如下:

import urllib.request

response = urllib.request.urlopen('www.baidu.com')
html = response.read()
print(html)
print("------------------------------------------------------------------")
print("------------------------------------------------------------------")
print(response.status)


运行结果:

Python 爬虫 urllib模块:get方式



2.decode的程序如下:

import urllib.request

response = urllib.request.urlopen('www.baidu.com')
html = response.read()

print(html.decode())
print("------------------------------------------------------------------")
print("------------------------------------------------------------------")
print(response.status)


运行结果:

<!DOCTYPE html>
<!--STATUS OK-->


<html>
<head>
    
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta content="always" name="referrer">
    <meta name="theme-color" content="#2932e1">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" />
    <link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg">


<link rel="dns-prefetch" href="//s1.bdstatic.com"/>
<link rel="dns-prefetch" href="//t1.baidu.com"/>
<link rel="dns-prefetch" href="//t2.baidu.com"/>
<link rel="dns-prefetch" href="//t3.baidu.com"/>
<link rel="dns-prefetch" href="//t10.baidu.com"/>
<link rel="dns-prefetch" href="//t11.baidu.com"/>
<link rel="dns-prefetch" href="//t12.baidu.com"/>
<link rel="dns-prefetch" href="//b1.bdstatic.com"/>
    
    <title>百度一下,你就知道</title>
    

<style id="css_index" index="index" type="text/css">html,body{height:100%}
.
.
.
.


</body>
</html>






------------------------------------------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
200


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI