[問題] lxml問題

看板Python作者 (藍.猴子)時間13年前 (2013/01/03 19:34), 編輯推噓5(501)
留言6則, 5人參與, 最新討論串1/1
大家好,初學python約1年, 這次使用lxml想要抓blog中的東西 但是有個地方一直無法抓成功 下面是網頁的原始碼 <div class="boxCategory1"> <a onClick="onclick_folder(document. getElementById('HiddenCategoryFolder_4362075'), document.getElementById('CategoryFolder_4362075'), 'http://l.yimg.com/e/serv/blog/img/', '1');" > <img id="CategoryFolder_4362075" src="http://l.yimg.com/e/serv/blog/img/plus.gif" /> 文字1</a> <a href="" rel="nofollow">http://www.xxx.xxx">文字2</a> </div> 我寫的code是這樣 allTitles = blogContent.xpath("//div[@class='boxcategory1']//a") for title in allTitles: print title.text 如果是像文字2就可以成功抓到印出來 但是文字1就無法 會是空白 請問是哪裡寫錯了呢@@" 謝謝指教 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.124.183.36

01/03 21:25, , 1F
用lxml ?
01/03 21:25, 1F
對 沒錯!

01/04 10:03, , 2F
試試pyquery吧 你會愛上的
01/04 10:03, 2F
謝謝推薦!!! 等下下課試試看!! ※ 編輯: ibluemonkey 來自: 140.124.183.36 (01/04 10:09) ※ 編輯: ibluemonkey 來自: 140.124.183.36 (01/04 10:11)

01/04 10:42, , 3F
試看看 list(title.itertext())
01/04 10:42, 3F
之後是用這個方法寫出來的 感謝!!!!!!

01/04 23:58, , 4F
中間夾 img tag
01/04 23:58, 4F

01/05 01:08, , 5F
title.xpath("text()")或是之前xpath("(前略)//a/text()")
01/05 01:08, 5F

01/06 23:38, , 6F
你沒注意階層的關係.. 你要的是print title.text_content()
01/06 23:38, 6F
也謝謝樓上三位大大的意見!!! ※ 編輯: ibluemonkey 來自: 140.124.183.36 (01/08 20:44)
文章代碼(AID): #1GvMpKrw (Python)
文章代碼(AID): #1GvMpKrw (Python)