[問題] 網頁爬蟲多個class疑問

看板Python作者 (阿洋)時間6年前 (2017/08/26 13:23), 編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/1
最近練習使用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

08/26 21:15, , 1F
findAll('td', {'class':['t3n1','t3r1']})
08/26 21:15, 1F

08/26 22:12, , 2F
感謝wennie大,可以抓到正負了,看來用字典似乎抓不到
08/26 22:12, 2F

08/26 22:12, , 3F
08/26 22:12, 3F
文章代碼(AID): #1PeGNCn2 (Python)