作者hazton (hazton)
看板Python
标题[问题] 关於用beautifulsoup抓资料一问
时间Sat Jul 4 19:54:35 2009
1.我想抓yahoo知识加的问题里的发问日期和解决日期
以下是我的程式码:
import re
import BeautifulSoup
url='
http://tw.knowledge.yahoo.com/question/question?qid=1609042207523 '
data = urllib.urlopen(url).read()
soup = BeautifulSoup.BeautifulStoneSoup(data)
Question_time = soup.table('td')
Question_start_time = Question_time[1]
Question_end_time = Question_time[2]
print Question_start_time
for i in re.compile('>(.+?)<').findall(Question_start_time):
print i
正常应该要印出 2009-06-14 19:25:38
不过却显示错误:TypeError: expected string or buffer
请问该如何解决这个错误呢??
2.我在1.的程式码底下试着写下
B_answer = soup.findAll('div',{'class':'main breakfix'})
Best_Answer = B_answer[1]
print Best_Answer[1:20]
如果是pirnt Best_Answer的话可以印出来,但如果後面加个[1:20]的话就会有错误
但我的语法应该是没什麽错才对,好像Best_Answer已经变成另一种型态的变数了
有人可以解释一下为什麽会这样吗?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.116.245.199
※ 编辑: hazton 来自: 140.116.245.199 (07/04 23:15)
1F:→ ia:1.语法有误要修改~url网址不存在 2.查div,用1的方法来写.. 07/05 02:38
2F:→ hazton:1的语法请问是哪里错了呢@@? 07/05 16:30
※ 编辑: hazton 来自: 140.116.245.199 (07/05 16:31)
※ 编辑: hazton 来自: 140.116.245.199 (07/05 21:49)
3F:→ hazton:另外查div的效果怪怪的...很难抓到什麽东西 07/05 21:49
4F:→ ia:给些提示~ 1.BeautifulSoup() 未用到 .. 所以 soup 从何来? 07/05 22:00
5F:→ ia:2.最佳解的 div id 是固定的.. 另外 BeautifulSoup 3.1.0 有bug 07/05 22:02
6F:→ ia:用旧版就可以避掉. 07/05 22:05
7F:→ hazton:哦 soup那行我少po上来了,程式码里有,我改一下@@a 07/06 02:07
※ 编辑: hazton 来自: 140.116.245.199 (07/06 02:08)