作者sgps50601 (阿洋)
看板Python
标题[问题] 网页爬虫多个class疑问
时间Sat Aug 26 13:23:22 2017
最近练习使用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://webptt.com/cn.aspx?n=bbs/Python/M.1503725004.A.C42.html
1F:推 wennie0433: findAll('td', {'class':['t3n1','t3r1']}) 08/26 21:15
2F:→ sgps50601: 感谢wennie大,可以抓到正负了,看来用字典似乎抓不到 08/26 22:12
3F:→ sgps50601: 。 08/26 22:12