作者coeric ( )
看板Python
標題[問題] SOUP內含奇怪的標籤?
時間Mon Jan 9 13:48:35 2017
<tr>
<th scope="row">機構院所大類:</th>
<td>合約醫療機構</td>
</tr>
<tr>
<th scope="row">醫院評鑑:</th>
<td>新制評鑑特優<BR/>新制教學評鑑優等</td>
</tr>
中間突然出現一個<BR/> 使用replace也無法處理掉
應該說,在find_all('td')時,回應:None.......
該如何處置?
----------------------------------------------------
po一下我的原始碼好了
from bs4 import BeautifulSoup
count=0
#全部
url="
http://www.health.taichung.gov.tw/lp.asp?CtNode=4639&CtUnit=3723&BaseDSD=7&mp=108010&nowPage=1&pagesize=2000"
#部份
#url="
http://www.health.taichung.gov.tw/lp.asp?CtNode=4639&CtUnit=3723&BaseDSD=7&mp=108010"
html=requests.get(url).content
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,"html.parser")
x=soup.find_all('tr')
for i in x[2:]:
j=i.find_all('td')
p_url='
http://www.health.taichung.gov.tw/'+j[1].a['href']
html_n=requests.get(p_url).content
soup_n = BeautifulSoup(html_n,"html.parser")
y=soup_n.table
item=y.find_all('table')
for br in soup.find_all('br'): br.extract()
item_title=item[0].find_all('th')
item_text=item[0].find_all('td')
temp={}
for j in range(len(item_title)):
key=(item_title[j].string).strip()[:-1]
value=(item_text[j].string)
print key, value
sleep(1)
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.8.102.255
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1483940918.A.2A3.html
→ penguin7272: for br in soup.find_all('br'): br.extract() 01/09 14:29
→ coeric: 感謝!!! 01/09 14:48
→ coeric: 可是為什麼find_all('td')時 01/09 15:21
→ coeric: 新制評鑑特優<BR/>新制教學評鑑優等 卻是None..... 01/09 15:22
→ penguin7272: 我用 find_all('td') 在原文可以找到兩個 td 欸? 01/09 15:55
原html內很多td..........0.0
※ 編輯: coeric (101.8.102.255), 01/09/2017 16:34:51
※ 編輯: coeric (101.8.102.255), 01/09/2017 16:37:39
→ zps: x=soup.find_all('td'); print(len(x)) => 6585個 01/09 20:30
→ zps: 不過我是python3,不知是否有差異,可試著用soup.prettify() 01/09 20:31
→ zps: 檢查看看,看真正讀到的是什麼 01/09 20:31