• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
BeyondTechnology
博客园    首页    新随笔    联系   管理    订阅  订阅

Beautiful Soup 解析html表格

from bs4 import BeautifulSoup
import urllib.request
doc = urllib.request.urlopen('http://www.bkzy.org/Index/Declaration?intPageNo=1')
doc = doc.read().decode('utf-8')

soup = BeautifulSoup(doc, "html.parser")

school = 0
pro_code = 1
pro_name = 2
xuewei = 3
pdf = 4


# find_all 查到所有tr列表
for tr in soup.find_all('tr',):
  # 在每个tr找td td = tr.find_all('td') try: print('%s_%s_%s_%s.pdf' % ( td[school].text.strip(), td[pro_code].text.strip(), td[pro_name].text.strip(), td[xuewei].text.strip()) ,td[pdf].find('a')['href']) except IndexError as e: pass
posted @ 2018-12-31 22:31  BeyondTechnology  阅读(855)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3