python爬虫

一、完善球赛程序

1.实例代码

a.预测球队比赛结果代码

 

def gameOver(a,b):
    if a>=10 and b>=10:
        if abs(a-b)==2:
            return True
    if a<10 or b<10:
        if a==11 or b==11:
            return True
    else:
        return False

 

b.编写一个Game.py

class Game():
    def __init__(self,a,b):
        self.a = a
        self.b = b

c.编写一个测试代码Testgame.py

import unittest
from game import Game
class GameTest(unittest.TestCase):
    def test_gameOver(self):
        self = Game('15','13')      
    def gameOver(a,b):
        if a>=10 and b>=10:
            if abs(a-b)==2:
                return True
        if a<10 or b<10:
            if a==11 or b==11:
                return True
        else:
            return False
#运行测试代码
unittest.main()

2.效果显示

二、访问网站20次,打印返回状态,text()内容,计算text()属性和content属性所返回网页内容的长度。

1.安装requests库

打开cnd,输入pip install requests,即可安装。

2.实例代码

import requests
for i in range(20):
    r = requests.get("https://www.sogou.com")#搜狗主页
    print("网页返回状态:{}".format(r.status_code))
print("text内容为:{}".format(r.text))
print("\n")
print("text内容长度为:{}".format(len(r.text)))
print("content内容长度为:{}".format(len(r.content)))

3.效果显示

 

posted on 2019-05-23 10:06  梁可乐  阅读(157)  评论(0编辑  收藏  举报

导航