作者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)
→ ia:1.語法有誤要修改~url網址不存在 2.查div,用1的方法來寫.. 07/05 02:38
→ hazton:1的語法請問是哪裡錯了呢@@? 07/05 16:30
※ 編輯: hazton 來自: 140.116.245.199 (07/05 16:31)
※ 編輯: hazton 來自: 140.116.245.199 (07/05 21:49)
→ hazton:另外查div的效果怪怪的...很難抓到什麼東西 07/05 21:49
→ ia:給些提示~ 1.BeautifulSoup() 未用到 .. 所以 soup 從何來? 07/05 22:00
→ ia:2.最佳解的 div id 是固定的.. 另外 BeautifulSoup 3.1.0 有bug 07/05 22:02
→ ia:用舊版就可以避掉. 07/05 22:05
→ hazton:哦 soup那行我少po上來了,程式碼裡有,我改一下@@a 07/06 02:07
※ 編輯: hazton 來自: 140.116.245.199 (07/06 02:08)