当前位置:   article > 正文

day19-爬虫和前端_java爬虫和前端

java爬虫和前端

爬虫 - 获取网络数据(通过各种互联网产品公开的数据)

爬虫过程

  1. 获取网络数据:requests、selenium
    1. 浏览器伪装
    2. 登录反爬
    3. 代理IP
  2. 解析数据(从获取到的网络数据中提取有效数据):正则表达式、基于css选择器的解析器(bs4)基于xpath的解析器(lxml)
  3. 保存数据:csv(excel)

网页 - html、css(css 选择器)

requests

  1. 什么是爬虫 - 就是获取网络数据(公开的数据)

    网络数据来源:网站对应的网页、手机App

  2. 爬虫基本流程

    第一步:获取网络数据(requests、selenium)
    第二步:解析数据 - 从获取到的网络数据中提取有效数据:正则表达式、基于css选择器的解析器(bs4)基于xpath的解析器(lxml)
    第三步:保存数据 - csv、excel、数据库等

  3. requests - python获取网络数据的第三方库(基于http或者https协议的网络请求)

    爬虫使用requests的两个场景:直接请求网页地址、对提供网页数据的数据接口发送请求

    requests基本用法

    # 1)对目标网页直接发送请求:
    # requests.get(网页地址) - 获取指定页面的数据返回一个响应对象
    import requests
    
    res = requests.get(
        url='https://cd.zu.ke.com/zufang',
        headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
        }
    )
    
    print(res)  # <Response [200]>      200 - 请求成功
    # 获取响应的状态码
    print(res.status_code)  # 200
    # 获取响应头
    print(
        res.headers)  # {'Server': 'Lianjia', 'Date': 'Thu, 11 Aug 2022 03:41:38 GMT', 'Content-Type': 'text/html; charset=UTF-8', 'Transfer-Encoding': 'chunked', 'Connection': 'close', 'Vary': 'Accept-Encoding', 'Set-Cookie': 'select_city=510100; expires=Fri, 12-Aug-2022 03:41:37 GMT; Max-Age=86400; path=/; domain=.ke.com, lianjia_ssid=46fea8f3-7445-406b-95e5-eaea697a5c95; expires=Thu, 11-Aug-22 04:11:37 GMT; Max-Age=1800; domain=.ke.com; path=/, lianjia_uuid=6fa834e5-6bb4-4b30-970a-6d9408e4b093; expires=Sun, 08-Aug-32 03:41:37 GMT; Max-Age=315360000; domain=.ke.com; path=/', 'Response-Request-Id': '8110988786433', 'via': 'web60-online.zeus.ljnode.com', 'Content-Encoding': 'gzip'}
    # 请求内容(返回的真正有用的数据)
    print(res.text)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    response.connect - 二进制类型的数据(图片、视频、音频等,例如:图片下载)
    response.text - 字符串类型的数据(网页)
    response.json - 对请求内容做完json解析后的数据(json数据接口)

html 基础

<!--
    !html是以标签为单位来给网页提供内容的。
    ?不同的标签可以提供不同的内容。

    1.标签的语法结构(重要!)1)双标签:<标签名 属性名1=属性值1 属性名2=属性值2  ...></标签名>
    2)单标签:<标签名 属性名1=属性值1 属性名2=属性值2  ...>或者<标签名 属性名1=属性值1 属性名2=属性值2  .../>

-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>zlq的网站</title>
    <link rel="icon" href="./img/笑脸.png">
</head>
<body>
    <h1>学生信息录入</h1>
    <img src="./img/笑脸.png",alt="">
    <p>请同学们完善信息</p>

    <span>芜湖</span>
    <a href="https://www.baidu.com/">百度</a>
    <br>
    姓名:<input type="text" value="憨憨">
    密码:<input type="password" name="" id="">
    <br><br>
    性别:
    <input type="radio" name="gender" id="g1"><label for="g1"></label>
    <input type="radio" name="gender" id="g2"><label for="g2"></label>
    <br><br>
    兴趣:
    <input type="checkbox">1
    <input type="checkbox">2
    <input type="checkbox">3
    <input type="checkbox">4

    <br>
    籍贯:
    <select name="" id="">
        <option value="北京">北京</option>
        <option value="成都">成都</option>
        <option value="西安">西安</option>
        <option value="憨憨">憨憨</option>
    </select>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

css基础

<!--
    css负责网页内容的样式(让网页可以变得更好看)
    css语法:
    选择器{属性1:属性值1;属性2:属性值2;...}

    选择器 - 选中需要添加样式的标签

    2.css选择器
    1)元素选择器(标签选择器) - 将标签名作为选择器,选中所有指定的标签
        p{} - 选中所有的p标签
        a{} - 选中所有的a标签
    2id选择器 -id属性值前面加#作为一个选择器,选择id属性值为指定值的标签
        每一个可见的标签都可以设置id属性,并且在一个网页中,同一个id值只有一个标签
        #p1 - 选中id属性值为p1的标签
        #a - 选中a属性值为p1的标签
    3)class选择器 -class属性值前加.作为一个选择器,选中class属性值为指定值的标签
        不同的标签可以有相同的class值;同一个标签可以有不同的class.c1 - 获取class值为c1的所有的标签
        P.c1 - 获取class值为c1的p标签
        .c1.c2 - 获取class值同时为c1和c2的标签
    4)群组选择器 - 将多个选择器用逗号隔开作为一个选择器
        p,a{} - 选中所有的p标签和所有的a标签
        #p1,.c1,p{} - 选中id为p1的标签和class为c1的标签和所有的p标签
    5)子代选择器 - 多个选择器用>隔开作为一个选择器
        div>p{}
    6)后代选择器 - 多个选择器用空格隔开作为一个选择器
        div p{}
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>页面二</title>
    <!--style标签中可以写css代码-->
    
</head>
<body>
    <!--1.元素选择器案例-->
    <!-- <style>
        p{
            color: red;
            font-size: 50px;
        }
    </style>
    <p>我是段落1</p>
    <a href="">链接</a>
    <div>我是div1</div>
    <p>我是段落2</p> -->
    <!--2.id选择器案例-->
    <!-- <style>
        #p2{
            color: green;
            background-color: yellow;
            font-size: 50px;
        }
    </style>
    <p>我是段落1</p>
    <a href="">链接</a>
    <div>我是div1</div>
    <p id="p2">我是段落2</p> -->
    <!--3.class选择器案例-->
    <!-- <style>
        .c1{
            color: green;
            background-color: yellow;
            font-size: 50px;
        }
        .c2{
            background-color: pink;
        }
        p.c1{
            text-decoration: line-through;
        }
        .c1.c2{
            border: 1px solid blue;
        }
    </style>
    /* 下面这个p标签有c1和c2两个class */
    <p class="c1 c2">我是段落1</p>
    <a class="c1 c2" href="">链接</a>
    <div>我是div1</div>
    <p id="p2">我是段落2</p>
    <a class="c1" href="">链接2</a>
    <p>我是段落2</p> -->
    <!--3.群组选择器案例-->
    <!-- <style>
        .c1,a{
            color: green;
            background-color: yellow;
            font-size: 50px;
        }
    </style>
    <p class="c1 c2">我是段落1</p>
    <a class="c1 c2" href="">链接</a>
    <div>我是div1</div>
    <p id="p2">我是段落2</p>
    <a class="c1" href="">链接2</a>
    <p>我是段落2</p> -->
    <style>
        p{
            color: red;
        }
        div>p{
            font-size: 30px;
        }
        div p {
            background-color: yellow;
        }
        #box1>span>p{
            text-decoration: line-through;
        }
        #box1>div p{
            text-decoration: line-through;
        }
    </style>
    <p>1</p> 
    <span>
        <p>5</p>
    </span>
    <div>
        <p>2</p>
    </div>
    <div id="box1">
        <p>3</p>
        <div>
            <span>
                <div>
                    <p>4</p>
                </div>
            </span>
        </div>
    </div>
</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/885945
推荐阅读
相关标签
  

闽ICP备14008679号