如果只是要提取一个标签 里面的属性值啥的,直接看这篇文章就可以了:
如果是标签的嵌套,可以参考下面的思路,虽然不是很简洁,但是可以解决你的问题:
可以看到不能直接 findAll 所有的 tr 标签,否则会有许多杂质的, 所以,可以知道 table, 根据id 或者class, 则可以唯一找到;
下面的重点就是如何分析出我们想要的数据,如何提取出 每个 tr 包含的一行数据呢?
我的思路是:
findALL---table => 得到一个list只包含一个table,也就是我要的table
将这个table 在用一次 findall 然后:
findAll --- tr ==> 得到一个list, 包含每一个
....; 好,我们已经得到我们要的每一行数据了如何提取每一个数据呢:两种方法:
第一种是 考虑遍历每个
, 利用findALL函数找 td, 读取td的string第二种是考虑遍历每个
, 利用findALL函数找到每个td, 用replace 替换掉 ,代码如下:
# -*- coding:utf-8 -*-
# python 2.7
# XiaoDeng
# http://tieba.baidu.com/p/2460150866
# 标签操作
from bs4 import BeautifulSoup
import urllib.request
import re
# 如果是网址,可以用这个办法来读取网页
# html_doc = "http://tieba.baidu.com/p/2460150866"
# req = urllib.request.Request(html_doc)
# webpage = urllib.request.urlopen(req)
# html = webpage.read()
html = """
(共49条/1页)
学年学期课程名称课程学分考试类型考试成绩所获学分考试成绩320163高等数学Ⅰ(一)5.5正常675.510735620171高等数学Ⅰ(二)5.5正常655.5111481"""
bs = BeautifulSoup(html, 'lxml')
score = bs.findAll('table', attrs={"id": 'ctl00_MainContentPlaceHolder_GridScore'})
bs2 = score[0] #提取出唯一一项,注意它依然是'lxml'格式,不需要重新beautifulSoup,注意它不是字符串!!!
score = bs2.findAll('tr') #找到每一个行
for i in score:
rt = i.findAll('td') #找到每一列
# print(rt)
if len(rt) == 0:
continue
## 一种方法:
for j in rt:
# print('j: ', j)
sj = str(j)
sj = sj.replace("
", '')sj = sj.replace('
', '')print(sj)
## 另一种方法:
for j in rt:
print(j.string)
print('=============================')