爬虫中国大学排名
from pip._vendor import requests
print('访问谷歌网站 获取Response对象')
r = requests.get("http://www.google.cn")
x = 1
while x <= 20:
print('第' + str(x) + '次的返回状态打印:' + str(r.status_code))
print('第' + str(x) + '次的text()打印:' + str(r.text))
print('第' + str(x) + '次的text()属性长度打印:' + str(len(r.text)))
print('第' + str(x) + '次的content属性长度打印:' +str(len(r.content)))
x += 1

(3)html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<h1>作业1</h1>
<p id="first">作业2</p>
</body>
<table border="1">
<tr>
<td>完成!</td>
<td>赞!</td>
</tr>
</table>
</html>
(4)爬中国大学排名网站内容
import requests
from bs4 import BeautifulSoup
allUniv = []
def getHTMLText(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = 'utf-8'
return r.text
except:
return ""
def fillUnivList(soup):
data = soup.find_all('tr')
for tr in data:
ltd = tr.find_all('td')
if len(ltd) == 0:
continue
singleUniv = []
for td in ltd:
singleUniv.append(td.string)
allUniv.append(singleUniv)
def printUnivList(num):
print("{1:^2}{2:{0}^10}{3:{0}^6}{4:{0}^4}{5:{0}^10}".format(chr(12288), "排名", "学校名称", "省市", "总分", "年费"))
for i in range(num):
u = allUniv[i]
print("{1:^4}{2:{0}^10}{3:{0}^5}{4:{0}^8.1f}{5:{0}^11}".format(chr(12288), u[0], u[1], u[2], eval(u[3]), u[11]))
def main():
url = 'https://www.gpnu.edu.cn/Guangdong polytechnic normal university2020.html'
html = getHTMLText(url)
soup = BeautifulSoup(html, "html.parser")
fillUnivList(soup)
printUnivList(10)
main()

浙公网安备 33010602011771号