找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 7623|回复: 10

[求助] 爬虫求助

1

主题

4

帖子

4

积分

贫民

积分
4
馨香莉莉 发表于 2017-7-22 15:23:33 | 显示全部楼层 |阅读模式
本帖最后由 馨香莉莉 于 2017-7-23 14:27 编辑

问题见图,希望大家可以帮助我,小白一个,谢谢!

问题见图

问题见图

university.py

1.72 KB, 下载次数: 2

回复

使用道具 举报

0

主题

20

帖子

20

积分

贫民

积分
20
sss123 发表于 2017-7-22 18:00:48 | 显示全部楼层
import  pandas,html5lib

dfs =pandas.read_html('http://www.zuihaodaxue.com/zuihaodaxuepaiming2017.html',attrs={'class':'table table-small-font table-bordered table-striped'},header=0)
for data in dfs:print(data)

你想解析的是 一个table  一个表 你可以试试用pandas 这个库 , 3行代码 比你那30行 简单的多
回复 支持 反对

使用道具 举报

1

主题

25

帖子

25

积分

贫民

积分
25
陈继塬 发表于 2017-7-23 02:01:17 | 显示全部楼层
能不能把源码发出来,不想打码。。
回复 支持 反对

使用道具 举报

3

主题

9

帖子

9

积分

贫民

积分
9
tangjunxiao 发表于 2017-7-23 11:16:21 | 显示全部楼层
换成2017的时候报什么错误?详细信息说一下
回复 支持 反对

使用道具 举报

1

主题

4

帖子

4

积分

贫民

积分
4
馨香莉莉  楼主| 发表于 2017-7-23 14:16:29 | 显示全部楼层
tangjunxiao 发表于 2017-7-23 11:16
换成2017的时候报什么错误?详细信息说一下

    print(tplt.format(u[0],u[1],u[2],chr(12288)))

TypeError: unsupported format string passed to NoneType.__format__
回复 支持 反对

使用道具 举报

1

主题

4

帖子

4

积分

贫民

积分
4
馨香莉莉  楼主| 发表于 2017-7-23 14:18:21 | 显示全部楼层
馨香莉莉 发表于 2017-7-23 14:16
print(tplt.format(u[0],u[1],u[2],chr(12288)))

TypeError: unsupported format string passed to  ...

import requests
from bs4 import BeautifulSoup#import the class of BeautifulSoup
import bs4#import the library of bs4

#获取网页信息的通用框架
def gethtmltext(url):
    try:
        r = requests.get(url,timeout=30)
        r.raise_for_status()
        r.endoding = r.apparent_encoding
        return r.text
    except:
        return ""
#填充列表
def fillunivlist(ulist,html):
    soup=BeautifulSoup(html,"html.parser")
    for tr in soup.find('tbody').children:
        '''检查网页代码可以发现数据都能存储在tbody
        标签中,这里需要对tbody的儿子节点进行遍历'''
        if isinstance(tr,bs4.element.Tag):
            '''delete the other element that incloud the 'tr' label
            to chose the suitable value'''
            tds = tr('td')
            #解析出tr标签后,将其存储在列表tds中
            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)))
    #定义输出变量tplt,\t为横向制表符,<为左对齐,10为每列的宽度
    #format()方法做出格式化输出
    for i in range(num):
        u = ulist
        print(tplt.format(u[0],u[1],u[2],chr(12288)))
        
def main():
    unifo = []
    url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2017.html'
    html = gethtmltext(url)
    fillunivlist(unifo,html)
    printunivlist(unifo,20)#chose 20 universities message
main()
回复 支持 反对

使用道具 举报

1

主题

4

帖子

4

积分

贫民

积分
4
馨香莉莉  楼主| 发表于 2017-7-23 14:28:10 | 显示全部楼层
tangjunxiao 发表于 2017-7-23 11:16
换成2017的时候报什么错误?详细信息说一下

代码我已经上传了
回复 支持 反对

使用道具 举报

3

主题

9

帖子

9

积分

贫民

积分
9
tangjunxiao 发表于 2017-7-24 17:29:06 | 显示全部楼层

建议看沙发的回复,用pandas 库
回复 支持 反对

使用道具 举报

2

主题

5

帖子

5

积分

贫民

积分
5
蟒蛇咬人好痛 发表于 2017-7-24 22:31:56 | 显示全部楼层
题主救我,我试着想用同样的代码爬“http://yz.chsi.com.cn/zsml/queryAction.do”这里的内容,但是报错。

报错内容为:Traceback (most recent call last):
  File "C:/Users/2233/Desktop/广东省研究生照收大学(医学).py", line 36, in <module>
    main()
  File "C:/Users/2233/Desktop/广东省研究生照收大学(医学).py", line 34, in main
    FillList(unifo , html)
  File "C:/Users/2233/Desktop/广东省研究生照收大学(医学).py", line 19, in FillList
    Slist.append([tds[0].string , tds[1].string , tds[2].string , tds[3].string , tds[4].string , tds[5].string ])
IndexError: list index out of range


下面是我的代码:
import requests
from bs4 import BeautifulSoup
import bs4
def GetHtmlTxt(url):
    try:
        r = requests.get(url , timeout = 30 )
        r.raise_for_status()
        r.recoding = r.apparent_encoding
        return r.text
    except:
        return ""


def FillList(Slist , html):
    soup = BeautifulSoup( html , "html.parser")
    for tr in soup.find("tbody").descendants:
        if isinstance(tr ,bs4.element.Tag ):
            tds = tr("td")
            Slist.append([tds[0].string , tds[1].string , tds[2].string , tds[3].string , tds[4].string , tds[5].string ])




def OutPutList(Slist , num):
    print("{:^10}\t{:^6}\t{:^10}\t{:3^}\t{:3^}\t{:3^}".format("招生大学","所在地址","学校特性","研究生院","计划","Doctor"))
    for i in range (num):
        u = Slist
        print("{:^10}\t{:^6}\t{:^10}\t{:3^}\t{:3^}\t{:3^}".format(u[0],u[1],u[2],u[3],u[4],u[5]))

def main():
    unifo = []
    url = "http://yz.chsi.com.cn/zsml/queryAction.do"
    html = GetHtmlTxt(url)
    FillList(unifo , html)
    OutPutList(unifo , 11 )
main()
回复 支持 反对

使用道具 举报

0

主题

25

帖子

25

积分

贫民

积分
25
zps26 发表于 2017-7-25 15:35:36 | 显示全部楼层
sss123 发表于 2017-7-22 18:00
import  pandas,html5lib

dfs =pandas.read_html('http://www.zuihaodaxue.com/zuihaodaxuepaiming2017.ht ...

这个简洁啊,不明白这是什么意思?
  1. 'class':'table table-small-font table-bordered table-striped'
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表