官方地址:http://www.crummy.com/software/BeautifulSoup
import requests
from bs4 import BeautifulSoup
r = requests.get("http://zx.caipiao.163.com/trend/ssq_basic.html?periodNumber=100")
soup = BeautifulSoup(r.text, "html5lib")
#body = soup.find("body")
data = soup.find(id="cpdata")
print type(data)
for i in data.find_all(align="center"):
print type(i)
if type(i) == type(data):
m = i.find_all("td",class_="chartBall02")
print type(m)#这就开始错了 类型不对
#不能依靠<></>去指示位置,只能靠撒网
blue = data.find_all("td",class_="chartBall02")
red = data.find_all("td",class_="chartBall01")
print type(blue)
import re
#有时候如果搜索条件过为复杂需要引入正则表达式
#ball = data.find_all(re.complie("chartBall0."))#你永远的不到想要的结构,因为这是属性里的数据,默认是<></>
ball = data.find_all(class_=re.compile("chartBall0."))
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最近感觉学了半天Python没写点啥挺对不起自己的,如是融汇了自己所学所长,学了一段网页抓去数据并做数据分析,结果........</span>
我被BeautifulSoup搞崩溃了,这个烂货居然在我find_all时候返回ResultSet对象,居然给我报错,我左右研究了一下,只有soup对象才能用find_all剩下的如果想要就得遍历list,一点都不美丽,罔我还去分析树结构
BeautifulSoup 的使用类似于一种对网页数据的分析原则,有时候由于标签并不全,很难一次性搞定所需要的,在这种情况下,复选以及特定条件的条件才行。
<body>
--><div class="main chart_list clearfix">
--><div style="position:relative;">
--><table id="chartsTable" class="data_table">
--><tbody id="cpdata">
--><tr >
--><td align="center" title="开奖日期:2014-06-10 星期二">2014065</td>
--><td class="chartBall07">03</td>
但是仔细想想也许越是趋近自然语言,越是这样吧,暂时说这些,等写完了再整理