在第一个列表的 Beautifulsoup return None 中查找函数
find function in Beautifulsoup return None in first list
我现在练习用 Beautifulsoup4 解析 HTML。
我在使用查找功能时遇到问题。
这是我的代码。
soup1 = BeautifulSoup(a,"html.parser")
tables1 = soup1.find('div', {'id':'auction_container'}).findAll('table')
for table in tables1:
if '매각기일' in table.get_text():
clue1 = table.find('td', {'class': 'head_con center'})
pro_clue1 = clue1.find('span', {'class':'bold'})
pro_clue2 = clue1.find('span',{'class':'no'})
clue2 = table.find('tr', {'valign': 'bottom'})
print(clue2.find('span', {'class': 'num'}))
变量a的pagesource太长,所以我在我的博客里写了完整的脚本。您可以像这样获取脚本。 http://blog.naver.com/khm2963/220983094160
当我执行这段代码时,我得到以下输出
None
<span class="num"><span class="f20">2015</span>타경<span class="f20">2321</span></span>
当我在 clue2.find('span', {'class': 'num'})
后面追加 .get_text ()
函数时,比如 print(clue2.find('span', {'class': 'num'}).get_text())
,我得到了下面的错误。
Traceback (most recent call last):
File "D:/python code/auction_crawl/test bs4.py", line 5895, in <module>
print(clue2.find('span', {'class': 'num'}).get_text())
AttributeError: 'NoneType' object has no attribute 'get_text'
如果我打印 print(clue2)
而没有 .find('span', {'class': 'num'})
我得到如下结果
<tr valign="bottom">
<td class="head_num left"><img alt="굿옥션로고" height="26"
src="/img/common/top_logo.gif" width="100"><span class="logo_pid"></span>
</img>
</td>
<td class="head_con center">
<span class="bold">서울남부지방법원 본원 8계(02-2192-1338)</span> / 매각기일 :
<span class="bold"><span class="no">2017.04.12(水)</span> <span class="no">
(10:00)</span>
</span></td>
</tr>
<tr valign="bottom">
<td class="head_num bold no left" style="width:190px;padding:10px 0 2px
0;font-size:15px"><span class="num"><span class="f20">2015</span>타경<span
class="f20">2321</span></span></td>
<td class="head_con center" style="padding-bottom:6px"><div>
<span class="ltblue"><img src="/img/icon/point_blue.gif" style="vertical-
align:middle"/></span> <span class="blue bold">서울남부지방법원 본원
</span> <span class="ltblue"><img src="/img/icon/point_blue.gif"
style="vertical-align:middle"/></span> 매각기일 : <span class="blue bold
no">2017.04.12(水) (10:00)</span> <span class="ltblue"><img
src="/img/icon/point_blue.gif" style="vertical-align:middle"/></span> <span
class="blue bold">경매 8계</span>(전화:02-2192-1338)</div>
</td>
</tr>
所以我将上面的 HTML 代码写入变量 d。并制作了另一个如下代码。
d = ''' HTML code above '''
soup4= BeautifulSoup(d,"html.parser")
clue = soup4.find('span', {'class': 'num'})
print(clue.get_text().strip())
当我激活上面的代码时,我得到了这样的响应2015타경2321
。
这就是我要的。
我想从顶部代码中获取 2015타경2321
。我怎样才能得到它??
您可以只验证您的 clue2.find('span', {'class': 'num'})
是否有结果,如果有,打印结果:
...
clue2number = clue2.find('span', {'class': 'num'})
if clue2number is not None:
print (clue2number.get_text(strip=True))
输出:
2015타경2321
我现在练习用 Beautifulsoup4 解析 HTML。 我在使用查找功能时遇到问题。 这是我的代码。
soup1 = BeautifulSoup(a,"html.parser")
tables1 = soup1.find('div', {'id':'auction_container'}).findAll('table')
for table in tables1:
if '매각기일' in table.get_text():
clue1 = table.find('td', {'class': 'head_con center'})
pro_clue1 = clue1.find('span', {'class':'bold'})
pro_clue2 = clue1.find('span',{'class':'no'})
clue2 = table.find('tr', {'valign': 'bottom'})
print(clue2.find('span', {'class': 'num'}))
变量a的pagesource太长,所以我在我的博客里写了完整的脚本。您可以像这样获取脚本。 http://blog.naver.com/khm2963/220983094160 当我执行这段代码时,我得到以下输出
None
<span class="num"><span class="f20">2015</span>타경<span class="f20">2321</span></span>
当我在 clue2.find('span', {'class': 'num'})
后面追加 .get_text ()
函数时,比如 print(clue2.find('span', {'class': 'num'}).get_text())
,我得到了下面的错误。
Traceback (most recent call last):
File "D:/python code/auction_crawl/test bs4.py", line 5895, in <module>
print(clue2.find('span', {'class': 'num'}).get_text())
AttributeError: 'NoneType' object has no attribute 'get_text'
如果我打印 print(clue2)
而没有 .find('span', {'class': 'num'})
我得到如下结果
<tr valign="bottom">
<td class="head_num left"><img alt="굿옥션로고" height="26"
src="/img/common/top_logo.gif" width="100"><span class="logo_pid"></span>
</img>
</td>
<td class="head_con center">
<span class="bold">서울남부지방법원 본원 8계(02-2192-1338)</span> / 매각기일 :
<span class="bold"><span class="no">2017.04.12(水)</span> <span class="no">
(10:00)</span>
</span></td>
</tr>
<tr valign="bottom">
<td class="head_num bold no left" style="width:190px;padding:10px 0 2px
0;font-size:15px"><span class="num"><span class="f20">2015</span>타경<span
class="f20">2321</span></span></td>
<td class="head_con center" style="padding-bottom:6px"><div>
<span class="ltblue"><img src="/img/icon/point_blue.gif" style="vertical-
align:middle"/></span> <span class="blue bold">서울남부지방법원 본원
</span> <span class="ltblue"><img src="/img/icon/point_blue.gif"
style="vertical-align:middle"/></span> 매각기일 : <span class="blue bold
no">2017.04.12(水) (10:00)</span> <span class="ltblue"><img
src="/img/icon/point_blue.gif" style="vertical-align:middle"/></span> <span
class="blue bold">경매 8계</span>(전화:02-2192-1338)</div>
</td>
</tr>
所以我将上面的 HTML 代码写入变量 d。并制作了另一个如下代码。
d = ''' HTML code above '''
soup4= BeautifulSoup(d,"html.parser")
clue = soup4.find('span', {'class': 'num'})
print(clue.get_text().strip())
当我激活上面的代码时,我得到了这样的响应2015타경2321
。
这就是我要的。
我想从顶部代码中获取 2015타경2321
。我怎样才能得到它??
您可以只验证您的 clue2.find('span', {'class': 'num'})
是否有结果,如果有,打印结果:
...
clue2number = clue2.find('span', {'class': 'num'})
if clue2number is not None:
print (clue2number.get_text(strip=True))
输出:
2015타경2321