我正在尝试从维基百科获得一些大学的lat lng,我有一个基本网址='https://de.wikipedia.org/wiki/Liste_altsprachlicher_Gymnasien'与大学列表,我来自href获取每个大学的维基页面以获得lat lng出现在他们的维基页面上。我收到此错误错误“NoneType”对象没有属性'text'“我无法纠正这一点,我在哪里做错了?
import time
import csv
from bs4 import BeautifulSoup
import re
import requests
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://de.wikipedia.org/wiki/Liste_altsprachlicher_Gymnasien')
html = driver.page_source
base_url = 'https://de.wikipedia.org'
url = 'https://de.wikipedia.org/wiki/Liste_altsprachlicher_Gymnasien'
res = requests.get(url)
soup = BeautifulSoup(res.text)
university = []
while True:
res = requests.get(url)
soup = BeautifulSoup(res.text)
links = soup.find_all('a', href=re.compile('.*\/wiki\/.*'))
for l in links:
full_link = base_url + l['href']
town = l['title']
res = requests.get(full_link)
soup = BeautifulSoup(res.text)
info = soup.find('span', attrs={"title":["Breitengrad","Längengrad"]})
latlong = info.text
university.append(dict(town_name=town, lat_long=latlong))
print(university)
修改1 感谢@rll我做了编辑:
if info is not None:
latlong = info.text
university.append(dict(town_name=town, postal_code=latlong))
print(university)
现在代码有效,但我只看到纬度而不是经度
示例输出:{'postal_code': '49°\xa072\xa036,73\xa0N', 'town_name': 'Schönborn-Gymnasium Bruchsal'}, {'postal_code': '49°\xa072\xa030,73\xa0N', 'town_name': 'St. Paulusheim'}
无论如何格式化这个输出以获得经度,并格式化输出抱歉我的正则表达式很差。
修改2
我还通过更新的代码
来获得经度info = soup.find('span', attrs={"title":"Breitengrad"})
info1 = soup.find('span',attrs={"title":"Längengrad"})
if info is not None:
latlong = info.text
longitude = info1.text
university.append(dict(town_name=town, postal_code=latlong,postal_code1=longitude))
print(university)
现在我的输出如下:
{'postal_code': '48°\xa045′\xa046,9″\xa0N',
'postal_code1': '8°\xa014′\xa044,8″\xa0O',
'town_name': 'Gymnasium Hohenbaden'},
所以我需要帮助格式化lat和long,因为我无法弄清楚如何转换例如:48°\xa045′\xa046,9″\xa0N to 48° 45′ 9″ N
感谢
答案 0 :(得分:1)
很抱歉没有直接回答,但我总是喜欢使用MediaWiki的API。我们很幸运在Python中使用了mwclient
,这使得使用API变得更加容易。
所以,对于它的价值,这是我用mwclient
做的方式:
import re
import mwclient
site = mwclient.Site('de.wikipedia.org')
start_page = site.Pages['Liste_altsprachlicher_Gymnasien']
results = {}
for link in start_page.links():
page = site.Pages[link['title']]
text = page.text()
try:
pattern = re.compile(r'Breitengrad.+?([0-9]+/[0-9]+/[\.0-9]+)/N')
breiten = [float(b) for b in pattern.search(text).group(1).split('/')]
pattern = re.compile(r'Längengrad.+?([0-9]+/[0-9]+/[\.0-9]+)/E')
langen = [float(b) for b in pattern.search(text).group(1).split('/')]
except:
continue
results[link['title']] = breiten, langen
这为查找坐标的每个链接提供了一个列表[deg, min, sec]
元组:
>>> results
{'Akademisches Gymnasium (Wien)': ([48.0, 12.0, 5.0], [16.0, 22.0, 34.0]),
'Akademisches Gymnasium Salzburg': ([47.0, 47.0, 39.9], [13.0, 2.0, 2.9]),
'Albertus-Magnus-Gymnasium (Friesoythe)': ([53.0, 1.0, 19.13], [7.0, 51.0, 46.44]),
'Albertus-Magnus-Gymnasium Regensburg': ([49.0, 1.0, 23.95], [12.0, 4.0, 32.88]),
'Albertus-Magnus-Gymnasium Viersen-Dülken': ([51.0, 14.0, 46.29], [6.0, 19.0, 42.1]),
...
}
您可以按照自己喜欢的方式进行格式化:
for uni, location in results.items():
lat, lon = location
string = """University {} is at {}˚{}'{}"N, {}˚{}'{}"E"""
print(string.format(uni, *lat+lon))
或将DMS坐标转换为十进制度:
def dms_to_dec(coord):
d, m, s = coord
return d + m/60 + s/(60*60)
decimal = {uni: (dms_to_dec(b), dms_to_dec(l)) for uni, (b, l) in results.items()}
注意,并非所有链接页面都可能是大学;我没仔细检查。