Python 如何获得文本组
我是第一次做海报。我正在制作一个程序,通过请求告诉天气,并美化设置正确信息格式的组。我有Python 如何获得文本组,python,python-3.x,beautifulsoup,output,output-formatting,Python,Python 3.x,Beautifulsoup,Output,Output Formatting,我是第一次做海报。我正在制作一个程序,通过请求告诉天气,并美化设置正确信息格式的组。我有 import requests from bs4 import BeautifulSoup url = 'https://weather.com/en-IN/weather/today/l/91ae3bca47e7dbeb23a6f4b10cb656d234e65f12a1e9b4dc80a0e02e5baa838c' def getdata(url): r = requests.get(ur
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)
程序正在输出当前温度:早晨-5°雪--
我希望它输出当前温度:-5°C,天空雪--
感谢您提供的任何帮助您的问题不太清楚,您正在进行预测并期待当前数据。这可能需要改进 当前:
live = soup.select_one('div.styles--card--3aeCQ a')
print(f"Current Temp: {live.find('span').get_text()}, Sky: {live.find('svg').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
网址是什么?网址是谢谢你的帮助。您是如何找到html中包含温度和降水值的行的?我正在努力学习bs4以及如何从网站上找到有用的值,但是我在找到任何对我有意义的东西时遇到了问题。很高兴能帮助我-通过使用chrome开发者工具检查源代码来找到行
Current Temp: -2°, Sky: Snow
Current Temp: -5°, Sky: Snow