爬虫爬取中国最好大学这个网页得出中国最好大学的排名,初入python,谢谢大佬

charmingzhong 2017-07-22 10:28:37
import requests
from bs4 import BeautifulSoup
import bs4


def getHTmlText(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""


def fillUnivList(ulist, html):
soup = BeautifulSoup(html, "html.parser")
for tr in soup.find('tbody').children:
if isinstance(tr, bs4.element.Tag):
tds = tr('td')
ulist.append([tds[0].string, tds[1].string, tds[2].string])


def printUnivList(ulist, num):
tplt = "{0:^10}\t{1:{3}^10}\t{2:^10}"
print(tplt.format("排名","学校名称","总分",chr(12288)))
for i in range(num):
u = ulist[i]
print(tplt.format(u[0],u[1],u[2],chr[12288]))


def main():
uinfo = []
url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2017.html'
html = getHTmlText(url)
fillUnivList(uinfo,html)
printUnivList(uinfo,20)
main()

以下是error(共三处):
Traceback (most recent call last):
File "G:/Python练习/untitled1/爬虫中国大学排名.py", line 38, in <module>
main()
File "G:/Python练习/untitled1/爬虫中国大学排名.py", line 37, in main
printUnivList(uinfo,20)
File "G:/Python练习/untitled1/爬虫中国大学排名.py", line 29, in printUnivList
print(tplt.format(u[0],u[1],u[2],chr[12288]))
TypeError: 'builtin_function_or_method' object is not subscriptable
排名    学校名称    总分

Process finished with exit code 1

番外:请大佬指教,初入python,谢谢
...全文
994 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangyigg316 2018-03-28
  • 打赏
  • 举报
回复
引用 9 楼 a_hua_er 的回复:
看到网上有人说是网址改了,然后我也试着去改了网址,可是错误依然存在
同在看视频,这个错误应该是说没有内容,就是爬取失败。 函数里:except: return"1" main里:直接print(html),结果为1,估计网页做了限制。 我也不会解决。
enol-stark 2018-03-07
  • 打赏
  • 举报
回复
看到网上有人说是网址改了,然后我也试着去改了网址,可是错误依然存在
enol-stark 2018-03-07
  • 打赏
  • 举报
回复
同样的代码,我的错误是这样的,大佬能教教么 跟着嵩天老师打的 Traceback (most recent call last): File "E:/无聊写python/untitled/最好大学排名——爬虫课堂.py", line 32, in <module> main() File "E:/无聊写python/untitled/最好大学排名——爬虫课堂.py", line 30, in main fillUnivList(unifo,html) File "E:/无聊写python/untitled/最好大学排名——爬虫课堂.py", line 14, in fillUnivList for tr in soup.find('tbody').children: #for循环遍历方法 AttributeError: 'NoneType' object has no attribute 'children' Process finished with exit code 1
chuifengde 2017-07-25
  • 打赏
  • 举报
回复
我看了一下,排名那里是网页的源文件问题,你右键点击网页,查看源文件,可以看到排名数字只有前面有个<td>后面少了一个</td>关闭标记,但是你用chrome的ctrl+shift+I时,排名后的</td>又被它补齐了。要取排名,我看要用正则吧
charmingzhong 2017-07-25
  • 打赏
  • 举报
回复
引用 2 楼 chuifengde 的回复:
print(tplt.format(u[0],u[1],u[2],chr[12288]))
这一行改成
print(tplt.format(u[0] or '',u[1],u[2],chr(12288)))
但是排名哪里没有信息出来
charmingzhong 2017-07-25
  • 打赏
  • 举报
回复
引用 6 楼 chuifengde 的回复:
你用的是bs4哪是正则
记错了,哈哈哈,
chuifengde 2017-07-25
  • 打赏
  • 举报
回复
你用的是bs4哪是正则
charmingzhong 2017-07-25
  • 打赏
  • 举报
回复
引用 4 楼 chuifengde 的回复:
我看了一下,排名那里是网页的源文件问题,你右键点击网页,查看源文件,可以看到排名数字只有前面有个<td>后面少了一个</td>关闭标记,但是你用chrome的ctrl+shift+I时,排名后的</td>又被它补齐了。要取排名,我看要用正则吧
我看也是差一个<td>,但是我不会弄,尴尬,我就是用的正则啊
chuifengde 2017-07-23
  • 打赏
  • 举报
回复
print(tplt.format(u[0],u[1],u[2],chr[12288]))
这一行改成
print(tplt.format(u[0] or '',u[1],u[2],chr(12288)))
charmingzhong 2017-07-22
  • 打赏
  • 举报
回复
大佬 等你们多时 不差这一会 愿春风把你送万里的地方送来

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧