猿问

网页抓取 python beautifulsoup:我如何提取网页的百分比

对于一个爱好项目,我正在尝试获取银行网站的利息百分比。我使用以下代码获取包含我需要的 % 的 html 标记。


from bs4 import BeautifulSoup

import requests 


response = requests.get('https://www.rabobank.nl/particulieren/hypotheek/hypotheekrente/rente-annuiteitenhypotheek-en-lineaire-hypotheek/')


soup = BeautifulSoup(response.text, 'html.parser')


soup.find_all(id = "idm1872399280")

现在我想按照网站上的说明提取每个类别的 % 并将它们写入 csv 文件。感谢您的帮助!


莫回无
浏览 165回答 1
1回答

青春有我

这似乎是一个重复的问题。此处的解决方案已修改以适合您的特定问题。潜在的解决方案:from bs4 import BeautifulSoupimport requests response = requests.get('https://www.rabobank.nl/particulieren/hypotheek/hypotheekrente/rente-annuiteitenhypotheek-en-lineaire-hypotheek/')soup = BeautifulSoup(response.text, 'html.parser')table = soup.find("div", attrs={"class": "tpa-table__scrollable-area"})rows = table.findAll('tr')data = [[td.findChildren(text=True) for td in tr.findAll("td")] for tr in rows]data = [[u"".join(d).strip() for d in l] for l in data]数据的输出看起来像这样[[u'',  u'Met NHG of tot en met 67,5% van de marktwaarde*',  u'Meer dan 67,5% tot en met 90% van de marktwaarde*',  u'Meer dan 90% van de marktwaarde*'], [u'1 jaar', u'1,20%', u'1,40%', u'1,60%'], [u'2 jaar', u'1,25%', u'1,45%', u'1,65%'], [u'3 jaar', u'1,35%', u'1,55%', u'1,75%'], [u'4 jaar', u'1,35%', u'1,55%', u'1,75%'], [u'5 jaar', u'1,40%', u'1,60%', u'1,80%'], [u'6 jaar', u'1,40%', u'1,60%', u'1,80%'], [u'7 jaar', u'1,40%', u'1,60%', u'1,80%'], [u'8 jaar', u'1,40%', u'1,60%', u'1,80%'], [u'9 jaar', u'1,40%', u'1,60%', u'1,80%'], [u'10 jaar', u'1,40%', u'1,60%', u'1,80%'], [u'11 jaar', u'1,55%', u'1,75%', u'1,95%'], [u'12 jaar', u'1,65%', u'1,85%', u'2,05%'], [u'13 jaar', u'1,75%', u'1,95%', u'2,15%'], [u'14 jaar', u'1,75%', u'1,95%', u'2,15%'], [u'15 jaar', u'1,75%', u'1,95%', u'2,15%'], [u'20 jaar', u'1,80%', u'2,00%', u'2,20%'], [u'25 jaar', u'2,05%', u'2,25%', u'2,45%'], [u'30 jaar', u'2,10%', u'2,30%', u'2,50%']]可以遍历数据以找到百分比。可以按如下方式访问示例列表元素In [4]:data[1][3]Out[5]: u'1,60%'
随时随地看视频慕课网APP

相关分类

Python
我要回答