python读取表格数据_请问 python 爬虫如何获取表格数据呢?

# -*- coding: utf-8 -*-

import requests

import sys

from lxml import etree

import pandas as pd

from multiprocessing.dummy import Pool as ThreadPool

reload(sys)

sys.setdefaultencoding("utf-8")

Name=[]

Money=[]

Project=[]

Date=[]

Style=[]

User_type=[]

page=[]

def spider(url):

html=requests.get(url).text

html=html.encode("utf-8")

selector = etree.HTML(html)

field=selector.xpath('//table[@class="table table-striped table-bordered"]')[0]

content=field.xpath('tr')

for each in content:

list=each.xpath('td')

name=list[0][0]

name=name.xpath('string(.)').replace('\t','').replace(' ','')

money=list[1].xpath('string(.)').replace('\t','').encode("utf-8")

money=money.split()[0]

project=list[2][0]

project=project.xpath('string(.)').replace('\t','').replace(' ','')

date=list[3][0]

date=date.xpath('string(.)').replace('\t','').replace(' ','')

style=list[4][0]

style=style.xpath('string(.)').replace('\t','').replace(' ','')

user_type=list[5][0]

user_type=user_type.xpath('string(.)').replace('\t','').replace(' ','')

Name.append(name)

Money.append(money)

Project.append(project)

Date.append(date)

Style.append(style)

User_type.append(user_type)

for i in range(1,101):

newpage='http://alumni.xjtu.edu.cn/donation/namelist?pageNo='+str(i)+'&pageSize=10&billnum=&donateUserName=&orderWay=&donationid=0'

page.append(newpage)

# 我的电脑是双核,所以下面是2,根据自己电脑情况来定,也可以把下面一句删了,运行会慢一点

pool = ThreadPool(2)

pool.map(spider,page)

data={'Name':Name,'Money':Money,'Project':Project,'Date':Date,'Style':Style,'User_type':User_type}

data=pd.DataFrame(data)

data.to_csv('donation.csv',encoding='gbk')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值