看板 Python 關於我們 聯絡資訊
最近練習使用beautiful soup爬公司營收網頁,發現這個表格里有兩個class,一個是正數t3n1,另一個t3r1來區分負數,於是嘗試用find_all再加上("td",{"class":"t3n1","class":"t3r1"})來抓取,卻都只能抓到正數t3n1,還在思考是否少了什麼,想詢問一下高手的見解,以下檢附程式碼。 (網頁片段) ... <td class="t3n0">102.3Q</td> <td class="t3n1">162,577</td> <td class="t3n1">83,636</td> <td class="t3n1">78,941</td> <td class="t3n1">48.56%</td> <td class="t3n1">59,618</td> <td class="t3n1">36.67%</td> <td class="t3r1">-268</td> <td class="t3n1">59,350</td> <td class="t3n1">51,952</td> ... (Python程式碼) url = 'http://fund.bot.com.tw/Z/ZC/ZCE/ZCE_2330.DJHTM' response = rq.get(url) html_doc = response.text # text 屬性就是 html 檔案 soup = BeautifulSoup(response.text, "lxml") # 指定 lxml 作為解析器 revence_id =[] #營收單位 revence_date =[] #日期 revence_main =[] #金額 price =soup.find_all("td",{"class":"t3n1","class":"t3r1"}) print(price) -- Sent from my Windows -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.115.88.26 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1503725004.A.C42.html
wennie0433: findAll('td', {'class':['t3n1','t3r1']}) 08/26 21:15
sgps50601: 感謝wennie大,可以抓到正負了,看來用字典似乎抓不到 08/26 22:12
sgps50601: 。 08/26 22:12