一、
使用request库的get()函数访问360搜索网页20次并且打印返回状态,text内容,计算text()属性和content()属性所返回网页内容的长度。
对360搜索主页进行爬虫:
利用request库的get函数访问google 20次,输入代码为:
import requests wan="https://www.so.com/" def pac(wan): print("第",i+1,"次访问") r=requests.get(wan,timeout=30) r.raise_for_status() print("text编码方式为",r.encoding) print("网络状态码为:",r.status_code) print("text属性:",r.text) print("content属性:",r.content) return r.text for i in range(20): print(pac(wan))
由于结果太长,这里将代码改为打印text属性和content属性的长度后展示最后一次访问的结果,代码改动:
print("text属性长度:",len(r.text)) print("content属性长度:",len(r.content))
第 20 次访问
text编码方式为 ISO-8859-1
网络状态码为: 200
text属性长度: 5294
content属性长度: 5294
360æç´¢
text编码方式为 ISO-8859-1
网络状态码为: 200
text属性长度: 5294
content属性长度: 5294

二、
这是一个简单的html页面,请保持为字符串,完成后面的计算要求。
a.打印head标签内容和你的学号后两位
b 获取body标签内容
c 获取id为first的标签对象
d 获取并打印html页面中的中文字符
html为:
1 2 3 4 5 6 7 8 9菜鸟教程(runoob.com) 10 11 12 13 14 15我的第一个标题学号25 16 17我的第一个段落。
18 19 20 21
row 1, cell 1 | 26 27row 1, cell 2 | 28 29
row 2, cell 1 | 34 35row 2, cell 2 | 36 37
菜鸟教程运行结果:
相关计算代码:
from bs4 import BeautifulSoup import re soup=BeautifulSoup('''菜鸟教程(runoob.com) 我的第一标题 我的第一个段落。
row 1, cell 1 row 1, cell 2 row 2, cell 1 row 2, cell 2