首页 > 其他 > 详细

第一个爬虫和测试

时间:2020-05-13 18:57:22      阅读:51      评论:0      收藏:0      [点我收藏+]

1.测试球赛的GameOver函数

def GameOver(N, scoreA, scoreB):

‘‘‘

function: 定义一局比赛的结束条件

N: 代表当前局次(第五局为决胜局)

return: 若比赛结束的条件成立返回真,否则为假

‘‘‘

if N <= 4:

return (scoreA>=25 and abs(scoreA-scoreB)>=2 or scoreB>=25 and abs(scoreA-scoreB)>=2)

else:

return (scoreA>=15 and abs(scoreA-scoreB)>=2) or (scoreB>=15 and abs(scoreA-scoreB)>=2)

 

try:

for i in range(10):

a,b,c=map(int,input().split(‘,‘))

print(GameOver(a,b,c))

except:

print("error")

2.用requests库的get()函数访问百度20次,打印返回状态,text()内容,计算text()属性和content属性所返回网页内容长度。

import requests
for i in(0,20):
r=requests.get("https://www.baidu.com/")
print(r.status_code)
print(r.text)
print(type(r.text))
print(type(r.content))
print(len(r.content))

-------------------------------------------------------------------------------------

200
<class ‘str‘>

<class ‘bytes‘>
2443

------------------------------------------------------------------------------------------------------------------------------

3.html库简单计算

----------------------------------------------原代码------------------------

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runboo.com) 26 </title>
</head>
<body>
<h1>我的第一个标题</h1>
<p id="first">我的第一个段落。</p >
</body>
<table border="1">
<tr>
<td>row 1,cell 1</td>
<td>row 1,cell 2</td>
</tr>
<tr>
<td>row 2,cell 1</td>
<td>row 2,cell 2</td>
</tr>
</table>
</html>

-------------------------------------------------------------------------------------------------------------------

 (首先我们先将原来的代码存在一个html文件中,这里的‘GB2312‘是百度的编码,如果不行换‘utf-8‘)

# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import re
path = ‘C:/Users/Administrator/AppData/Roaming/Heinote/text.html‘
htmlfile = open(path, ‘r‘, encoding=‘GB2312‘)
htmlhandle = htmlfile.read()
soup=BeautifulSoup(htmlhandle, "html.parser")
print(soup.head,"41")
print(soup.body)
print(soup.find_all(id="first"))
r=soup.text
pattern = re.findall(‘[\u4e00-\u9fa5]+‘,r)
print(pattern)

------------------------------------------------结果-------------------------------------------------------------

<head>

<meta charset="utf-8"/>

<title>菜鸟教程(runboo.com) 26 </title>
</head> 41
<body>
<h1>我的第一个标题</h1>
<p id="first">我的第一个段落。</p>
</body>
[<p id="first">我的第一个段落。</p>]
[‘菜鸟教程‘, ‘我的第一个标题‘, ‘我的第一个段落‘]

----------------------------------------------------结果--------------------------------------------------------------------------------

4.

第一个爬虫和测试

原文:https://www.cnblogs.com/1qwe/p/12883126.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!