首页 > 解决方案 > 从段落中提取整数

问题描述

我正在尝试仅从该段中提取费用金额,但我遇到了问题。有两笔费用,我想要其中两笔。这是我的代码:http://www.reading.ac.uk/ready-to-study/study/subject-area/modern-languages-and-european-studies-ug/ba-spanish-and-history。 aspx

fees_div = soup.find('div', class_='Fees hiddenContent pad-around-large tabcontent')
if fees_div:
    fees_list = fees_div.find_all('\d+','p')
    course_data['Fees'] = fees_list
    print('fees : ', fees_list)

标签: pythonweb-scrapingbeautifulsoupreweb-scraping-language

解决方案


试一试:

import re
import requests
from bs4 import BeautifulSoup

r = requests.get('http://www.reading.ac.uk/ready-to-study/study/subject-area/modern-languages-and-european-studies-ug/ba-spanish-and-history.aspx')
soup = BeautifulSoup(r.text,'html.parser')
item = soup.find(id='Panel5').text
fees = re.findall(r"students:[^£]+(.*?)[*\s]",item)
print(fees)

输出:

['£9,250', '£17,320']

推荐阅读