我的第一个爬虫实验

一、python 连接测试URL

  1. 运行环境: python3.7  win7x64
  2. 使用工具: VS Code
  3. python 第三方库: requests (自行安装 >>> cmd --->pip install requests, 具体不做介绍)
  4. requests 库简介
  5. ① 导入库
  6. from requests import get

    ② 设定url, 并使用get方法请求页面得到响应

    url = "http://www.baidu.com"
    r = get(url, timeout=3)
    print("获得响应的状态码:", r.status_code)
    print("响应内容的编码方式:", r.encoding)

    运行结果:

    获得响应的状态码: 200
    响应内容的编码方式: ISO-8859-1

     ③ 获取网页内容

    url_text = r.text
    print("网页内容:", r.text)
    print("网页内容长度:", len(url_text))

    运行结果:

    网页内容: <!DOCTYPE html><!--STATUS OK--><html> <head> ... &nbsp;京ICP证030173号&nbsp; ... </body> </html>

    网页内容长度: 2381

    ④ 重新获取网页内容

    r.encoding = "utf-8"
    url_text = r.text
    print("网页内容:", r.text)
    print("网页内容长度:", len(url_text))

    运行结果:

    网页内容: <!DOCTYPE html> <!--STATUS OK--><html> <head> ... 意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

    网页内容长度: 2287

    二、python 疯狂连接URL

    1.  使用上述的连接测试 "经验", 找个网站进行疯狂连接
    2. 在此,选取搜狗网站
    3. 上代码
    复制代码
    url = "http://www.sogou.com" # 搜狗
    for i in range(200):
        print("Test %d:" % (i+1), end=" ")
        response = get(url, timeout=5)
        # 判断连接状态
        if response.status_code == 200:
            print("Conncect successful!")
        else:
            print("Conncect UNsuccessful!")
    复制代码

    运行结果:

    Test 1: Conncect successful!
    Test 2: Conncect successful!
    Test 3: Conncect successful!
    Test 4: Conncect successful!
    Test 5: Conncect successful!

                        ................

    Test 199: Conncect successful!
    Test 200: Conncect successful!

    三、获取网页的各个属性标签内容

    (我也不知道这个标题什么意思, 感觉很牛但看不懂, 才怪呢!你肯定看得懂!)

    这里,选取一个很厉害的网站做演示

    URL = https://www.runoob.com/

    步骤说明:

    1. 找个url, 上面有了, 其实随便一个都是OK的

    2. 抓取网页内容,这个上面有详解,不难

    3. 本次使用 BeautifulSoup 第三方库,需要自行下载【详情介绍

    4. 开工

      前面提供了 URL,现在抓取网页内容

    复制代码
     1 # -*- encoding:utf-8 -*-
     2 from requests import get
     3 def getText(url):
     4     try:
     5         r = get(url, timeout=5)
     6         r.raise_for_status()
     7         r.encoding = 'utf-8'
     8         return r.text
     9     except Exception as e:
    10         print("Error:", e)
    11         return ''
    from bs4 import BeautifulSoup
    1 url = "https://www.runoob.com/"
    2 html = getText(url)
    3 soup = BeautifulSoup(html)

    ① 获取 head 标签

    print("head:", soup.head)
    print("head:", len(soup.head))

        由于结果比较多,就只输出第二个结果

    head: 33

    ② 获取 body 标签

    print("body:", soup.body)
    print("body:", len(soup.body))

      由于结果比较多,就只输出第二个结果

    body: 39

    ③ 获取 title 标签

    print("title:", soup.title)

    title: <title>菜鸟教程 - 学的不仅是技术,更是梦想!</title>

    ④ 获取 title 的内容

    print("title_string:", soup.title.string)

    title_string: 菜鸟教程 - 学的不仅是技术,更是梦想!

    ⑤ 查找特定 id 的内容

    print("special_id:", soup.find(id='cd-login'))

    special_id: <div id="cd-login"> <!-- 登录表单 -->
    <div class="cd-form">
    <p class="fieldset"> ......

    ⑥ 查找所有的 a 标签

    a: [<a href="/">菜鸟教程 -- 学的不仅是技术,更是梦想!</a>, <a class="current" data-id="index" href="//www.runoob.com/" title="菜鸟教程">首页</a>, ......

    ⑦ 摘取所有的中文字符

    复制代码
    1 import re
    2 def getChinese(text):
    3     text_unicode = text.strip() # 将字符串进行处理, 包括转化为unicode
    4     string = re.compile('[^u4e00-u9fff]')
    5         # 中文编码范围是 u4e00-u9ffff
    6         # 中文、数字编码范围是 u4e00-u9fa50
    7     chinese = "".join(string.split(text_unicode))
    8     return chinese
    复制代码
    print("Chinese:", getChinese(html))
原文地址:https://www.cnblogs.com/0609hlz/p/10929758.html