gpt4 book ai didi

python - 无法从网页中获取一些分散的项目

转载 作者:行者123 更新时间:2023-11-28 16:56:51 25 4
gpt4 key购买 nike

我正在尝试使用 python 从网页中获取四个字段,但问题是我要获取的数据不在任何结构化 html 中,因此我找不到任何方法来单独获取它们。

webpage address

我试过:

import re
import requests
from bs4 import BeautifulSoup

link = 'https://colegios.es/2012/cra-la-gaznata-san-bartolome-de-pinares/'

def get_content(link):
res = requests.get(link,headers={'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(res.text,"lxml")
school_name = soup.select_one("h1 > a").get_text(strip=True)
school_address = soup.find("p",text=re.compile('Dirección:\s*([^"]*?)')).text
school_phone = soup.find("p",text=re.compile('Tel\.\s*(.*?)\s*')).text
print(school_name,school_address,school_phone)

if __name__ == '__main__':
get_content(link)

我得到的真是一团糟:

CRA La Gaznata  San Bartolomé de Pinares CRA La Gaznata Servicios:   Jornada contínua,  Educación Infantil y Primaria   Público Dirección:    del Pino, 2  5267  San Bartolomé de Pinares Ávila  Tel. 920 270 070   Fax 920 270 070 05005981@educa.jcyl.es [google-map-v3 addmarkerlist=”del Pino, 2  5267  San Bartolomé de Pinares Ávila {}5-default.png”] CRA La Gaznata Servicios:   Jornada contínua,  Educación Infantil y Primaria   Público Dirección:    del Pino, 2  5267  San Bartolomé de Pinares Ávila  Tel. 920 270 070   Fax 920 270 070 05005981@educa.jcyl.es [google-map-v3 addmarkerlist=”del Pino, 2  5267  San Bartolomé de Pinares Ávila {}5-default.png”]

我希望获取的输出(第二个是名称内可用的郊区):

CRA La Gaznata 
San Bartolomé de Pinares
del Pino, 2 5267 San Bartolomé de Pinares Ávila
920 270 070

如何从该网页获取四个字段?

最佳答案

关键是将解析器更改为 html5lib , 这样 <br>标签将被 get_text() 正确翻译成换行符 |方法 - 然后使用 re 更容易解析文本:

import re
import requests
from bs4 import BeautifulSoup

link = 'https://colegios.es/2012/cra-la-gaznata-san-bartolome-de-pinares/'

def get_content(link):
res = requests.get(link,headers={'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(res.text,"html5lib")

text = soup.select_one('.post-content > p').get_text(strip=True, separator='\n')

school_name, suburb = soup.select_one("h1 > a").get_text(strip=True, separator='\n').split('\n')
school_address = re.findall(r'Dirección:\s*(.*)', text)[0]
school_phone = re.findall(r'Tel\.\s*([\d\s]+\d)', text)[0]
email = re.findall(r'[^\s]+@[^\s]+', text)[0]

print(school_name)
print(suburb)
print(school_address)
print(school_phone)
print(email)

if __name__ == '__main__':
get_content(link)

打印:

CRA La Gaznata
San Bartolomé de Pinares
del Pino, 2 5267 San Bartolomé de Pinares Ávila
920 270 070
05005981@educa.jcyl.es

关于python - 无法从网页中获取一些分散的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57598324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com