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

网页抓取python beautifulsoup:如何提取网页的百分比,python,web-scraping,Python,Web Scraping,对于一个爱好项目,我正试图从一家银行网站上赚取%的利息。我使用以下代码获得包含我需要的%I的html标记 from bs4 import BeautifulSoup import requests response = requests.get('https://www.rabobank.nl/particulieren/hypotheek/hypotheekrente/rente-annuiteitenhypotheek-en-lineaire-hypotheek/') soup = B

对于一个爱好项目,我正试图从一家银行网站上赚取%的利息。我使用以下代码获得包含我需要的%I的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文件。
谢谢你的帮助

这似乎是一个重复的问题。根据您的具体问题修改了中的解决方案


潜在解决方案:

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')
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%'

这似乎是一个重复的问题。根据您的具体问题修改了中的解决方案


潜在解决方案:

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')
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%'