python读取html table_【转】Python——读取html的table内容

这篇博客介绍了如何使用Python的lxml库和pandas库解析HTML页面中的表格内容。通过示例代码,展示了如何获取表格中的教师姓名、职称和主页链接,并将这些数据整理成DataFrame进行存储。
摘要由CSDN通过智能技术生成

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

from lxml.html import parse

from urllib.request import urlopen

import pandas as pd

# 可能爬的这个网页比较特殊,需要写下面两句话

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

# 根据链接获得整个html放到doc中

parsed = parse(urlopen('https://info.zufe.edu.cn/xygk/szdw.htm'))

doc = parsed.getroot()

#读取html中的table

# 用列表来存老师名字

all_teachers=[]

# 用字典保存主页链接

link_dic={}

# 用字典保存职称

zhicheng={}

# 找到html中有

的所有table,以列表的形式返回给tables

tables = doc.findall('.//table')

# 我们要的是第一个table

content=tables[0].text_content()

tds = tables[0].findall('.//td')

# 一条条遍历所有td里的内容

for td in tds:

# 判断当前属于哪个职称,再给zc赋值

zhi=td.findall('.//strong')

i

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值