Python 从已删除数据列表中的每个单词中选择第一个字符
我一直在努力简化使用bs4收集的一些数据 我正在尝试缩写以下内容的输出:Python 从已删除数据列表中的每个单词中选择第一个字符,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我一直在努力简化使用bs4收集的一些数据 我正在尝试缩写以下内容的输出: import urllib.request from bs4 import BeautifulSoup url = "http://www.bbc.co.uk/weather/en/2644037/?day1" page = urllib.request.urlopen(url) soup = BeautifulSoup(page, "html5lib") weekWeather = soup.find('div', {
import urllib.request
from bs4 import BeautifulSoup
url = "http://www.bbc.co.uk/weather/en/2644037/?day1"
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page, "html5lib")
weekWeather = soup.find('div', {'class':'daily-window'})
wD = [x.text for x in weekWeather.findAll('span', {'class':'description blq-hide'})]
输出是一个列表
['South South Westerly', 'South Westerly', 'Southerly', 'Southerly', 'Southerly']
我想缩写为['SSW',SW','S','S','S']
我的第一个计划是使用split()
我有一种感觉,这是因为数据返回的方式
任何指针都很好,谢谢。以最简单的形式,您可以通过.split()
按空格分割,并获得每个单词的第一个字符:
["".join([item[0] for item in x.text.split()])
for x in weekWeather.select('span.description.blq-hide')]
这将返回:
['SSW', 'SW', 'S', 'S', 'S']
可能重复的