zoukankan      html  css  js  c++  java
  • 第一个爬虫和测试

    Python测试函数的方法之一是用:try……except

    复制代码
    def gameover(a,b):
        if a>=10 and b>=10 and abs(a-b)==2:
            return True
        if (a>=11 and b<11) or (a<11 and b>=11):
            return True
        return False
    try:
        a=gameover(10,11)
        print(a)
    except:
        print("Error")
    复制代码

    gameover测试的函数,没传参数的a,b,函数结果是True or False

    try:试着执行gameover()函数,正常就执行函数

    except:否则 打印'Error'

    这里用10,11这一对来测试,结果为:

    runfile('D:/新建文件夹/chesi.py', wdir='D:/新建文件夹')
    True

    程序运行正常且结果正确

    若不输入参数,结果应为Error,结果为:

    打开360搜索20次

    用requests()打开360搜索

    代码如下:

    from requests import *
    try:
        for i in range(20):
            r=get("https://www.so.com/")
            r.raise_for_status()
            r.encoding='utf-8'
            print(r)
        print(len(r.text))
        print(len(r.content))
    except:
        print("Error")

    结果成功显示200 

    成功

    下面有一段html代码;

    <!DOCTYPE html>

    <html>

    <head>

    <meta charset='utf-8'>

    <title>菜鸟教程(runoob.com)</title>

    </head> <body>

    <h1>我的第一标题</h1>

    <p  id='frist'>我的第一段落。</p>

    </body>

    </table>

    </html>

    我们需要做以下要求:

     

    我们有以下代码解决:

    复制代码
    from bs4 import BeautifulSoup
    import re
    html = BeautifulSoup("<!DOCTYPE html>
    <html>
    <head>
    <meta charset='utf-8'>
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    <h1>我的第一标题</h1>
    <p  id='frist'>我的第一段落。</p>
    </body>
    </table>
    </html>","html.parser")
    print(html.head,"20") 
    print(html.body)
    print(html.find_all(id="first")) 
    r=html.text
    pattern = re.findall(u'[u1100-uFFFDh]+?',r)
    print(pattern)
  • 相关阅读:
    HDU 5486 Difference of Clustering 图论
    HDU 5481 Desiderium 动态规划
    hdu 5480 Conturbatio 线段树 单点更新,区间查询最小值
    HDU 5478 Can you find it 随机化 数学
    HDU 5477 A Sweet Journey 水题
    HDU 5476 Explore Track of Point 数学平几
    HDU 5475 An easy problem 线段树
    ZOJ 3829 Known Notation 贪心
    ZOJ 3827 Information Entropy 水题
    zoj 3823 Excavator Contest 构造
  • 原文地址:https://www.cnblogs.com/zhoukun520/p/10908683.html
Copyright © 2011-2022 走看看