• 1

  • 作业一
    git地址:https://github.com/yaya19910111/homework_hgws.git
    代码节选:
    main.py

    def goto_stocks(self):
    self.find(By.XPATH, "//*[@text='行情' and contains(@resource-id, 'tab')]").click()
    return Stocks(self._driver)

    stocks.py

    from selenium.webdriver.common.by import By

    from test_appium.page.base_page import BasePage
    from test_appium.page.search import Search


    class Stocks(BasePage):

    def stocks_goto_search(self):
    self.find(By.ID,"action_search").click()
    return Search(self._driver)

    def get_stocks_name(self):
    return self.find(By.ID, "portfolio_stockName").text

    test_stocks.py

    from test_appium.page.app import App

    class TestStocks:
    def setup(self):
    self.stocks=App().start().main().goto_stocks()

    def test_stocks_goto_search(self):
    assert '已添加' in self.stocks.stocks_goto_search().search('jd').add_select().get_msg()
    self.stocks.goto_back()
    assert '京东'==self.stocks.get_stocks_name()
  • 第 11 期_appium 进阶_20200223 at 2020年02月23日

    作业一:

    from time import sleep

    from appium import webdriver
    from appium.webdriver.common.mobileby import MobileBy
    from appium.webdriver.common.touch_action import TouchAction
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait


    class TestXueqiu:
    def setup(self):
    caps = {}
    caps["platformName"] = "Android"
    caps["deviceName"] = "test"
    caps["appPackage"] = "com.xueqiu.android"
    caps["appActivity"] = ".view.WelcomeActivityAlias"
    caps["automationName"]="UiAutomator2"
    # caps['dontStopAppOnReset']=True
    # caps["noReset"]=True
    caps["unicodeKeyboard"] = True
    caps["chromedriverExecutable"] = "/Users/myname/webdriver/chromedriver_v2.20"

    self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
    self.driver.implicitly_wait(30)

    def teardown(self):
    sleep(10)
    self.driver.quit()

    def test_webview_2(self):
    self.driver.find_element(By.XPATH, "//*[@text='交易' and contains(@resource-id, 'tab')]").click()

    WebDriverWait(self.driver, 30).until(lambda x: len(self.driver.contexts) > 1)

    self.driver.switch_to.context(self.driver.contexts[-1])
    self.driver.find_element(By.CSS_SELECTOR, ".trade_home_xueying_SJY").click()

    WebDriverWait(self.driver, 30).until(lambda x: len(self.driver.window_handles) > 3)
    self.driver.switch_to.window(self.driver.window_handles[-1])

    phone = (By.XPATH, "//input[@placeholder='请输入手机号']")
    ver_code = (By.XPATH, "//input[@placeholder='请输入验证码']")
    button = (By.CSS_SELECTOR, '.open_form-submit_1Ms')
    js_toast = (By.CSS_SELECTOR, '.Toast_toast_22U')
    close = (By.ID, 'action_bar_close')
    back = (By.ID, 'action_bar_back')

    self.driver.switch_to.window(self.driver.window_handles[-1])

    WebDriverWait(self.driver, 60).until(expected_conditions.visibility_of_element_located(phone))

    self.driver.find_element(*phone).send_keys("15600534760")
    self.driver.find_element(*ver_code).send_keys("1234")
    self.driver.find_element(*button).click()

    WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(js_toast))
    assert '请输入正确的验证码!' in self.driver.find_element(*js_toast).text

    self.driver.switch_to.window(self.driver.window_handles[0])
    self.driver.switch_to.context(self.driver.contexts[0])

    # 判断页面有没有关闭这个控件,没有就点击后退
    if self.driver.find_elements(*close) == []:
    self.driver.find_element(*back).click()
    print("点击了返回")
    else:
    self.driver.find_element(*close).click()
    print("点击了关闭")


  • 作业一:

    #寻找build-toolsadb
    2020-02-19 15:38:58:847 [ADB] Found 1 'build-tools' folders under '/Users/myname/Library/Android/sdk' (newest first):
    2020-02-19 15:38:58:848 [ADB] /Users/myname/Library/Android/sdk/build-tools/29.0.3
    2020-02-19 15:38:58:848 [ADB] Using 'adb' from '/Users/myname/Library/Android/sdk/platform-tools/adb'
    #连接android驱动
    2020-02-19 15:38:58:849 [ADB] Trying to find a connected android device
    2020-02-19 15:38:58:850 [ADB] Getting connected devices...
    2020-02-19 15:38:58:863 [ADB] Connected devices: [{"udid":"emulator-5554","state":"device"}]
    #设置驱动id=emulator-5554
    2020-02-19 15:38:58:866 [ADB] Using 'adb' from '/Users/myname/Library/Android/sdk/platform-tools/adb'
    2020-02-19 15:38:58:866 [ADB] Setting device id to emulator-5554
    #获取当前系统的SDK版本号:27
    2020-02-19 15:38:58:867 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell getprop ro.build.version.sdk'
    2020-02-19 15:38:58:892 [ADB] Current device property 'ro.build.version.sdk': 27
    2020-02-19 15:38:58:892 [ADB] Device API level: 27
    #检查设备能否正常响应adb命令
    2020-02-19 15:38:58:893 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 wait-for-device'
    2020-02-19 15:38:58:901 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell echo ping'
    #检查安装settings app,安装前先判断是否已经存在于设备上
    2020-02-19 15:38:58:918 [ADB] Getting install status for io.appium.settings
    2020-02-19 15:38:58:919 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.settings'
    2020-02-19 15:38:58:945 [ADB] 'io.appium.settings' is installed
    2020-02-19 15:38:58:945 [ADB] Getting package info for 'io.appium.settings'
    2020-02-19 15:38:58:946 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.settings'
    2020-02-19 15:38:58:991 [ADB] The version name of the installed 'io.appium.settings' is greater or equal to the application version name ('3.0.1' >= '3.0.1')
    2020-02-19 15:38:58:991 [ADB] There is no need to install/upgrade '/usr/local/lib/node_modules/appium/node_modules/_io.appium.settings@3.0.1@io.appium.settings/apks/settings_apk-debug.apk'
    2020-02-19 15:38:58:992 [ADB] Getting IDs of all 'io.appium.settings' processes
    2020-02-19 15:38:58:992 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell 'pgrep --help; echo $?''
    2020-02-19 15:38:59:013 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell pgrep -f io\\.appium\\.settings'
    2020-02-19 15:38:59:044 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell appops set io.appium.settings android\:mock_location allow'
    2020-02-19 15:38:59:091 [ADB] Forwarding system: 8200 to device: 6790
    2020-02-19 15:38:59:092 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 forward tcp\:8200 tcp\:6790'
    #安装io.appium.uiautomator2.server
    2020-02-19 15:38:59:107 [ADB] Getting install status for io.appium.uiautomator2.server
    2020-02-19 15:38:59:108 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.uiautomator2.server'
    2020-02-19 15:38:59:135 [ADB] 'io.appium.uiautomator2.server' is installed
    2020-02-19 15:38:59:136 [ADB] Getting package info for 'io.appium.uiautomator2.server'
    2020-02-19 15:38:59:136 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.uiautomator2.server'
    2020-02-19 15:38:59:169 [ADB] The version name of the installed 'io.appium.uiautomator2.server' is greater or equal to the application version name ('4.5.5' >= '4.5.5')
    2020-02-19 15:38:59:170 [ADB] Checking app cert for /usr/local/lib/node_modules/appium/node_modules/_appium-uiautomator2-server@4.5.5@appium-uiautomator2-server/apks/appium-uiautomator2-server-v4.5.5.apk
    #使用apksignerapk进行签名
    2020-02-19 15:38:59:172 [ADB] Using 'apksigner' from '/Users/myname/Library/Android/sdk/build-tools/29.0.3/apksigner'
    2020-02-19 15:38:59:172 [ADB] Starting '/Users/myname/Library/Android/sdk/build-tools/29.0.3/apksigner' with args '["verify","--print-certs","/usr/local/lib/node_modules/appium/node_modules/_appium-uiautomator2-server@4.5.5@appium-uiautomator2-server/apks/appium-uiautomator2-server-v4.5.5.apk"]'
    2020-02-19 15:38:59:988 [ADB] apksigner stdout: Signer #1 certificate DN: EMAILADDRESS=android@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US
    2020-02-19 15:38:59:989 [ADB] Signer #1 certificate SHA-256 digest: a40da80a59d170caa950cf15c18c454d47a39b26989d8b640ecd745ba71bf5dc
    2020-02-19 15:38:59:989 [ADB] Signer #1 certificate SHA-1 digest: 61ed377e85d386a8dfee6b864bd85b0bfaa5af81
    2020-02-19 15:38:59:990 [ADB] Signer #1 certificate MD5 digest: e89b158e4bcf988ebd09eb83f5378e87
    2020-02-19 15:38:59:990 [ADB]
    2020-02-19 15:38:59:991 [ADB] '/usr/local/lib/node_modules/appium/node_modules/_appium-uiautomator2-server@4.5.5@appium-uiautomator2-server/apks/appium-uiautomator2-server-v4.5.5.apk' is already signed.
    #检查安装io.appium.uiautomator2.server.test
    2020-02-19 15:38:59:991 [ADB] Getting install status for io.appium.uiautomator2.server.test
    2020-02-19 15:38:59:992 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package io.appium.uiautomator2.server.test'
    2020-02-19 15:39:00:023 [ADB] 'io.appium.uiautomator2.server.test' is installed
    #检查apk证书
    2020-02-19 15:39:00:023 [ADB] Checking app cert for /usr/local/lib/node_modules/appium/node_modules/_appium-uiautomator2-server@4.5.5@appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk
    2020-02-19 15:39:00:024 [ADB] Starting '/Users/myname/Library/Android/sdk/build-tools/29.0.3/apksigner' with args '["verify","--print-certs","/usr/local/lib/node_modules/appium/node_modules/_appium-uiautomator2-server@4.5.5@appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk"]'
    2020-02-19 15:39:00:479 [ADB] apksigner stdout: Signer #1 certificate DN: EMAILADDRESS=android@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US
    2020-02-19 15:39:00:479 [ADB] Signer #1 certificate SHA-256 digest: a40da80a59d170caa950cf15c18c454d47a39b26989d8b640ecd745ba71bf5dc
    2020-02-19 15:39:00:479 [ADB] Signer #1 certificate SHA-1 digest: 61ed377e85d386a8dfee6b864bd85b0bfaa5af81
    2020-02-19 15:39:00:480 [ADB] Signer #1 certificate MD5 digest: e89b158e4bcf988ebd09eb83f5378e87
    2020-02-19 15:39:00:480 [ADB]
    2020-02-19 15:39:00:480 [ADB] '/usr/local/lib/node_modules/appium/node_modules/_appium-uiautomator2-server@4.5.5@appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk' is already signed.
    #列出所有的测试包
    2020-02-19 15:39:00:484 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell pm list instrumentation'
    #检查安装雪球app
    2020-02-19 15:39:00:821 [ADB] Getting install status for com.xueqiu.android
    2020-02-19 15:39:00:821 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys package com.xueqiu.android'
    2020-02-19 15:39:00:853 [ADB] 'com.xueqiu.android' is installed
    #使用am停止应用,pm清除应用数据
    2020-02-19 15:39:00:854 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell am force-stop com.xueqiu.android'
    2020-02-19 15:39:00:899 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell pm clear com.xueqiu.android'
    2020-02-19 15:39:01:337 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell am force-stop io.appium.uiautomator2.server.test'
    2020-02-19 15:39:01:387 [ADB] Creating ADB subprocess with args: ["-P",5037,"-s","emulator-5554","shell","am","instrument","-w","io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner"]
    2020-02-19 15:39:03:537 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell dumpsys window'
    2020-02-19 15:39:03:561 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell am start -W -n com.xueqiu.android/.view.WelcomeActivityAlias -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000'
    2020-02-19 15:39:20:922 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 shell am force-stop com.xueqiu.android'
    2020-02-19 15:39:21:080 [ADB] Removing forwarded port socket connection: 8200
    2020-02-19 15:39:21:081 [ADB] Running '/Users/myname/Library/Android/sdk/platform-tools/adb -P 5037 -s emulator-5554 forward --remove tcp\:8200'

    作业二和作业三:

    from time import sleep

    from appium import webdriver
    from appium.webdriver.common.mobileby import MobileBy


    class TestXueqiu:
    def setup(self):
    caps = {}
    caps["platformName"] = "Android"
    caps["deviceName"] = "test"
    caps["appPackage"] = "com.xueqiu.android"
    caps["appActivity"] = ".view.WelcomeActivityAlias"
    caps["automationName"]="UiAutomator2"
    # caps["noReset"]=False

    self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
    self.driver.implicitly_wait(10)

    def teardown(self):
    sleep(10)
    self.driver.quit()

    #作业二
    def test_search(self):
    self.driver.find_element(MobileBy.ID,"tv_search").click()
    self.driver.find_element(MobileBy.ID,"search_input_text").send_keys("阿里巴巴")
    self.driver.find_element(MobileBy.XPATH,"//*[@text='09988']").click()
    assert float(self.driver.find_element(MobileBy.ID,'current_price').text)>200

    #作业三
    def test_join(self):
    # 获取加自选的股票名称
    example=self.driver.find_element(MobileBy.ID,"stock_name_one").text

    #添加一个涨幅第一的股票
    self.driver.find_element(MobileBy.ID,"stock_one").click()
    self.driver.find_elements(MobileBy.ID,"floating_action_image_view_id")[3].click()
    self.driver.find_element(MobileBy.ID,"tv_left").click()

    #返回首页
    self.driver.find_element(MobileBy.ID,"action_back").click()

    #搜索这个股票
    self.driver.find_element(MobileBy.ID, "tv_search").click()
    self.driver.find_element(MobileBy.ID, "search_input_text").send_keys(example)
    self.driver.find_element(MobileBy.ID, "code").click()
    # assert "已添加"==self.driver.find_element(MobileBy.ID,"followed_btn").get_attribute("text")
    follow_button=self.driver.find_element(MobileBy.XPATH, '//*[@text="%s"]/../../android.widget.LinearLayout[3]/android.widget.TextView'%example)
    assert 'com.xueqiu.android:id/followed_btn' ==follow_button.get_attribute('resource-id')
  • 好的,收到。以后会注意的。

  • 作业地址:https://github.com/yaya19910111/homework_hgws.git
    基于课程贴中的代码进行了修改和添加

  • 课间作业1
    进入testerhome,访问社团,访问霍格沃兹测试学院,访问最顶部的第一个帖子。把代码贴到回复里。

    # Generated by Selenium IDE
    import pytest
    import time
    import json
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

    class TestTestsele():
    def setup_method(self, method):
    self.driver = webdriver.Chrome()
    self.vars = {}

    def teardown_method(self, method):
    self.driver.quit()

    def test_testsele(self):
    self.driver.get("https://testerhome.com/")
    self.driver.set_window_size(1421, 944)
    self.driver.find_element(By.LINK_TEXT, "社团").click()
    self.driver.find_element(By.LINK_TEXT, "霍格沃兹测试学院").click()
    self.driver.find_element(By.CSS_SELECTOR, ".topic-21848 .title > a").click()

    课间作业2
    进入testerhome,访问MTSC2020置顶帖,点击目录,点击议题征集范围。把代码贴到回复里。

    import time

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait


    class TestCase:

    def wait_click(self, element):
    WebDriverWait(self.driver, 10).until(
    expected_conditions.element_to_be_clickable(element)
    )

    def setup_method(self):
    self.driver = webdriver.Chrome()
    self.driver.implicitly_wait(3)
    self.driver.get('https://testerhome.com/')

    def teardown_method(self):
    time.sleep(3)
    self.driver.quit()

    def test_page(self):
    element1 = (By.CSS_SELECTOR, 'button.btn.btn-default')
    element2 = (By.LINK_TEXT, '议题征集流程')
    self.driver.find_element(By.PARTIAL_LINK_TEXT, 'MTSC2020 中国互联网测试开发大会议题征集').click()
    self.wait_click(element1)
    self.driver.find_element(*element1).click()
    self.wait_click(element2)
    self.driver.find_element(*element2).click()

    课后作业3
    企业微信自动添加成员,需要复用已经登录的chrome,需要debugger address,代码贴到回复里
    需要在后台开启debug-adress,扫码登录企业微信:

    /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
    import os
    import time

    import pytest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait


    class TestWinXin:

    def setup_method(self):
    #多浏览器支持
    browser = os.getenv("browser","").lower()
    print(browser)

    if browser == "headless":
    self.driver = webdriver.PhantomJS()
    elif browser == "firefox":
    self.driver = webdriver.Firefox()
    else:
    options=webdriver.ChromeOptions()
    #使用debug
    options.debugger_address='127.0.0.1:9222'
    self.driver = webdriver.Chrome(options=options)

    self.driver.implicitly_wait(3)

    def teardown_method(self):
    time.sleep(3)
    self.driver.quit()

    @pytest.mark.parametrize("username,acctid,phone",
    [('test1','test1','15282064333'),
    ('test2','test2','15282064333')])
    def test_weixin_add(self,username,acctid,phone):
    """
    添加成员
    :return:
    """

    self.driver.get('https://work.weixin.qq.com/wework_admin/frame#index')
    self.driver.find_element(By.LINK_TEXT, "添加成员").click()
    element =(By.CSS_SELECTOR,'#username')
    WebDriverWait(self.driver,10).until(expected_conditions.visibility_of_element_located(element))
    self.driver.find_element(*element).send_keys(username)
    self.driver.find_element(By.CSS_SELECTOR, '#memberAdd_acctid').send_keys(acctid)
    self.driver.find_element(By.CSS_SELECTOR, '#memberAdd_phone').send_keys(phone)
    self.driver.find_element(By.LINK_TEXT, "保存" ).click()
  • 十一期_Python 测试_20191222 at 2019年12月22日

    🌸
    1、课间作业一:

    def test_div_1():
    """
    b>0
    :return:
    """

    assert div(1,1)==1

    def test_div_2():
    """
    b=0
    :return:
    """

    assert div(1,0)

    def test_div_3():
    """
    b<0
    :return:
    """

    assert div(1,-1)==-1

    def test_div_4():
    """
    a>0
    :return:
    """

    assert div(1,1)==1

    def test_div_5():
    """
    a=0
    :return:
    """

    assert div(0,1)==0

    def test_div_6():
    """
    a<0
    :return:
    """

    assert div(-1,1)==-1

    2、课后作业2:

    import pytest
    from test_pytest.div import div


    # 整数运算
    @pytest.mark.parametrize("num1,num2,expectation", [(10, 5, 2), (10, -5, -2), (-8, 4, -2), (-8, -4, 2), (0, 2, 0)])
    def test_div_int(num1, num2, expectation):
    assert div(num1, num2) == expectation


    # 浮点运算
    @pytest.mark.parametrize("num1,num2,expectation",
    [(1, 2, 0.5),
    (1, 3, 0.33333333),
    (2, 3, 0.66666667),
    (8.88, 1.33, 6.67669173),
    (8.88, 2, 4.44),
    (8, 2.2, 3.63636364),
    (8, 0.5, 16)])
    def test_div_float(num1, num2, expectation):
    assert div(num1, num2) == expectation


    # 入参不为数字
    @pytest.mark.parametrize("num1,num2", [('a', 5), (1, 'b'), ('hello', 'world')])
    def test_div_non_num(num1, num2):
    assert div(num1, num2)


    # zero
    @pytest.mark.parametrize("num1,num2,expectation", [(1, 0, None), (0.5, 0, None), (0, 0, None)])
    def test_div_zero(num1, num2, expectation):
    assert div(num1, num2) == expectation

    报告截图:

  • 重邮的还可以投吗?

  • 课间作业1:从testerhome获取首页的代码,并统计有多少个帖子

    curl 'https://testerhome.com/' |grep -n '.*<a title=".*" href=".*">.*</a>$' |grep -v img |wc -l

    答案:30

    课间作业2:使用bing搜索引擎搜索shell,并给出bing的检索结果数,换成纯数字, 把代码和结果贴到回复里

    curl 'https://cn.bing.com/search?q=shell' > bing.txt
    grep -o "[0-9, ]*条结果" bing.txt |awk -F' ' '{print $1}' |sed 's/\,/''/g'

    答案:79100000

    拓展:返回的结果改为万返回

    grep -o "[0-9, ]*条结果" bing.txt |awk -F' ' '{print $1}' |sed -E -e 's/\,/''/g' -e 's/[0]{4,4}$//'

    答案:7910万

    课后作业3:(看完课后解答,觉得自己写得不够简练优美,决定改下自己的回答😂 😀
    演练web日志 /tmp/nginx.log
    1、找出log中的404 500的记录总共有多少条

    grep -E -o "^[0-9]*.[0-9]*.[0-9]*.[0-9]* - - \[.*\] \".*\" (404|500) " /tmp/nginx.log |wc -l
    #改正后
    awk '$9~/404|500/' /tmp/nginx.log |wc -l

    答案:267条

    2、找出访问量最高的前三名ip

    grep -E -o "^[0-9]*.[0-9]*.[0-9]*.[0-9]*" /tmp/nginx.log | sort | uniq -c |sort -rn |head -n 3
    #改正后
    awk '{print $1}' /tmp/nginx.log |sort |uniq -c |sort -rn |head -3
    #答案:
    282 216.244.66.241
    130 136.243.151.90
    110 127.0.0.1

    3、聚类所有的url,比如把/topics/1234 /topics/2434 都聚类成 /topics/id,去掉变化的数字等元素,聚类完成后取出top10的访问量最高的路由地址

    grep -E -o "(GET|POST|HEAD|PUT|DELETE|TRACE|OPTIONS|CONNECT).*HTTP"  /tmp/nginx.log | awk -F' ' '{print $2}' |sed -E -e 's/\?.*//g' -e 's/\/[0-9]+\//\/id\//g' -e 's/\/[0-9]+$/\/id/g' -e 's/\/[0-9]+\./\/id\./g' |sed -E -e 's#\/id\/.*(jpg|png|gif|jpeg).*#\/id#g' | sort | uniq -c | sort -rn |head -n 10
    #改正后
    awk '{print $7}' /tmp/nginx.log |sed -E \
    -e 's#\?.*##g' \
    -e 's#/[0-9]+[/|\.]#/id/#g' \
    -e 's#/[0-9]+$#/id#g' \
    -e 's#/[0-9a-z\-]*.(jpg|png|gif|jpeg).*$#/id#g' \
    | sed -E \
    -e 's#/topics/id#/topics/id/udid#' \
    -e 's#/([^/]*)/(topics|following|replies|followers|reward|favorites|columns|id)$#/userid/\2#' \
    -e 's#^/(topic|cable|id)$#/\1/udid#' \
    -e 's#^/[^/]+$#/userid#' \
    -e 's#/topics/node[0-9]+#/topic/nodeid#' \
    -e 's#/udid##' \
    |sort |uniq -c |sort -rn |head -10

    答案:

    584 /cable
    372 /topics/id
    195 /userid
    147 /topics/id/replies/id/edit
    94 /
    61 /uploads/photo/userid/id
    55 /_img/uploads/photo/userid/id
    49 /topics/id/replies/id/reply_suggest
    46 /userid/replies
    41 /topics/id/show_wechat