[問題] Beautifulsoup find_all 找不到符合標籤

看板Python作者 (使單力)時間9年前 (2016/05/05 18:23), 編輯推噓0(009)
留言9則, 2人參與, 最新討論串1/1
各位大大好: 小弟目前在學寫python+beautifulsoup+lxml 目前有個練習是讀取一份清單中的資料,來此資料再讀取、辨斷某個xml資料中是否有符合的tag存在: 比如xml中只有<centos>: <centos> <name>centos</name> <version>7</version> <download-url>http://ftp.ksu.edu.tw/pub/CentOS/7/isos/x86_64/CentOS-7-x86_64-DVD-1511.iso</download-url> </centos> 並用下面的code讀取xml檔案後並嘗試判斷是否有找到或找不到對應的tag: from bs4 import BeautifulSoup soup = BeautifulSoup(open(os.xml)) os = "fedora" for item in soup.findAll( os ): print item.tag,item,attrib if item == "": print "OS %s not exist in DB" else: print "OS %s exist in DB" 看起來並不會執行,如果 os = "centos",就能抓到對應的資料,但 os = "fedora"不行 求教: 1,如何判斷才是正確的方法?bs的網站說,findall在找不到tag時會return空字串,但我不太理解如何可以判斷空字串?看起來用 == ""是不行的。 2,另外執行python時會有下列錯誤,這個如何解決? /usr/local/lib/python2.7/site-packages/bs4/__init__.py:166: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently. To get rid of this warning, change this: BeautifulSoup([your markup]) to this: BeautifulSoup([your markup], "html.parser") markup_type=markup_type)) 我有先爬過文,各種解決都不行,比如 BeautifulSoup(markup, "xml") 抱歉問的可能是很基本的問題,感謝指導:D -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.194.30.193 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1462443807.A.61D.html

05/05 19:22, , 1F
不是就因為沒有fedora這個標籤嗎
05/05 19:22, 1F

05/05 19:24, , 2F
空的時候是回傳空陣列吧, 不是空字串
05/05 19:24, 2F

05/05 19:26, , 3F
而且應該是soup.findAll(os) == [] 不是item
05/05 19:26, 3F

05/05 23:57, , 4F
可以用len()來判斷list是否為空
05/05 23:57, 4F

05/06 00:04, , 5F
出現Warning是因為你沒有註明使用何種Parser,所以他預
05/06 00:04, 5F

05/06 00:05, , 6F
設使用內建的html.parser並跳出警告,但html.parser不支
05/06 00:05, 6F

05/06 00:06, , 7F
援xml,所以你需要安裝lxml套件,才可以使用
05/06 00:06, 7F

05/06 00:06, , 8F
BeautifulSoup(markup, "lxml")或
05/06 00:06, 8F

05/06 00:06, , 9F
BeautifulSoup(markup, "xml")
05/06 00:06, 9F
文章代碼(AID): #1NAnyVOT (Python)
文章代碼(AID): #1NAnyVOT (Python)