如何只获取文本 BeautifulSoup
How to just get text BeautifulSoup
我是第一次发贴。我正在制作一个程序来告诉天气,请求和 BeautifulSoup 格式化正确的信息是错误的。我有
import requests
from bs4 import BeautifulSoup
url = 'https://weather.com/en-IN/weather/today/l/91ae3bca47e7dbeb23a6f4b10cb656d234e65f12a1e9b4dc80a0e02e5baa838c'
def getdata(url):
r = requests.get(url)
return r.text
htmldata = getdata(
"https://weather.com/en-IN/weather/today/l/91ae3bca47e7dbeb23a6f4b10cb656d234e65f12a1e9b4dc80a0e02e5baa838c")
soup = BeautifulSoup(htmldata, 'html.parser')
precip = soup.find('div', id='WxuTodayWeatherCard-main-486ce56c-74e0-4152-bd76-7aea8e98520a')
currentPrecip = precip.find('li')
for j in currentPrecip:
showPrecip = currentPrecip.get_text()
result = f"Current Temp: {showPrecip}"
print(result)
程序正在输出Current Temp: Morning-5°Snow--
,我希望它输出Current Temp: -5°C, Sky Snow
感谢任何帮助并提前致谢
您的问题不是很清楚,您进行预测并期望当前数据。这可能应该改进。
当前:
live = soup.select_one('div.styles--card--3aeCQ a')
print(f"Current Temp: {live.find('span').get_text()}, Sky: {live.find('svg').get_text()}")
预报(上午):
morning = soup.select_one('section[data-testid="TodayWeatherModule"] ul>li')
print(f"Current Temp: {morning.div.span.get_text()}, Sky: {morning.find('title').get_text()}")
例子
import requests
from bs4 import BeautifulSoup
url = 'https://weather.com/en-IN/weather/today/l/91ae3bca47e7dbeb23a6f4b10cb656d234e65f12a1e9b4dc80a0e02e5baa838c'
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
live = soup.select_one('div.styles--card--3aeCQ a')
print(f"Current Temp: {live.find('span').get_text()}, Sky: {live.find('svg').get_text()}")
morning = soup.select_one('section[data-testid="TodayWeatherModule"] ul>li')
print(f"Current Temp: {morning.div.span.get_text()}, Sky: {morning.find('title').get_text()}")
输出
Current Temp: -2°, Sky: Snow
Current Temp: -5°, Sky: Snow
我是第一次发贴。我正在制作一个程序来告诉天气,请求和 BeautifulSoup 格式化正确的信息是错误的。我有
import requests
from bs4 import BeautifulSoup
url = 'https://weather.com/en-IN/weather/today/l/91ae3bca47e7dbeb23a6f4b10cb656d234e65f12a1e9b4dc80a0e02e5baa838c'
def getdata(url):
r = requests.get(url)
return r.text
htmldata = getdata(
"https://weather.com/en-IN/weather/today/l/91ae3bca47e7dbeb23a6f4b10cb656d234e65f12a1e9b4dc80a0e02e5baa838c")
soup = BeautifulSoup(htmldata, 'html.parser')
precip = soup.find('div', id='WxuTodayWeatherCard-main-486ce56c-74e0-4152-bd76-7aea8e98520a')
currentPrecip = precip.find('li')
for j in currentPrecip:
showPrecip = currentPrecip.get_text()
result = f"Current Temp: {showPrecip}"
print(result)
程序正在输出Current Temp: Morning-5°Snow--
,我希望它输出Current Temp: -5°C, Sky Snow
感谢任何帮助并提前致谢
您的问题不是很清楚,您进行预测并期望当前数据。这可能应该改进。
当前:
live = soup.select_one('div.styles--card--3aeCQ a')
print(f"Current Temp: {live.find('span').get_text()}, Sky: {live.find('svg').get_text()}")
预报(上午):
morning = soup.select_one('section[data-testid="TodayWeatherModule"] ul>li')
print(f"Current Temp: {morning.div.span.get_text()}, Sky: {morning.find('title').get_text()}")
例子
import requests
from bs4 import BeautifulSoup
url = 'https://weather.com/en-IN/weather/today/l/91ae3bca47e7dbeb23a6f4b10cb656d234e65f12a1e9b4dc80a0e02e5baa838c'
soup = BeautifulSoup(requests.get(url).text, 'html.parser')
live = soup.select_one('div.styles--card--3aeCQ a')
print(f"Current Temp: {live.find('span').get_text()}, Sky: {live.find('svg').get_text()}")
morning = soup.select_one('section[data-testid="TodayWeatherModule"] ul>li')
print(f"Current Temp: {morning.div.span.get_text()}, Sky: {morning.find('title').get_text()}")
输出
Current Temp: -2°, Sky: Snow
Current Temp: -5°, Sky: Snow