作者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://webptt.com/cn.aspx?n=bbs/Python/M.1483940918.A.2A3.html
2F:→ penguin7272: for br in soup.find_all('br'): br.extract() 01/09 14:29
3F:→ coeric: 感谢!!! 01/09 14:48
4F:→ coeric: 可是为什麽find_all('td')时 01/09 15:21
5F:→ coeric: 新制评监特优<BR/>新制教学评监优等 却是None..... 01/09 15:22
6F:→ 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
7F:→ zps: x=soup.find_all('td'); print(len(x)) => 6585个 01/09 20:30
8F:→ zps: 不过我是python3,不知是否有差异,可试着用soup.prettify() 01/09 20:31
9F:→ zps: 检查看看,看真正读到的是什麽 01/09 20:31