推 wjyo: 他股號股名在script元素裡面,你前面extract把他排除掉了 03/19 19:54
爬蟲網址:
https://fubon-ebrokerdj.fbs.com.tw/z/zg/zgb/zgb0.djhtm?a=1160&b=1163&c=E&d=1
使用下面語法爬蟲 (語法自己摸索可能寫的不是很好,也可以指正)
def get_NBA(url):
r = requests.get(url=url)
r.encoding = 'big5'
if r.status_code == requests.codes.ok:
soup = BeautifulSoup(r.text, 'html.parser')
[s.extract() for s in soup("option")]
[s.extract() for s in soup("script")]
[s.extract() for s in soup("SELECT")]
[s.extract() for s in soup("input")]
[s.extract() for s in soup("SCRIPT")]
for tr in soup.find_all('tr'):
for td in tr.findAll('td'):
text = '#'+td.getText()
file.write(text)
file.write('#EENNDD\n')
#file.write('\n')
結果遇到他在主要股號股名那邊我就抓不到了
<tr>
<td class="t4t1" nowrap id="oAddCheckbox">
<SCRIPT LANGUAGE=javascript>
<!--
GenLink2stk('AS2834','臺企銀');
//-->
</SCRIPT>
</td>
<td class="t3n1" nowrap>215</td>
<td class="t3n1" nowrap>9</td>
<td class="t3n1" nowrap>206</td>
</tr>
不知是否有方法可以爬到
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.228.55.124 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1647654597.A.D25.html