Python试图从weather websi获取段落

2024-04-19 18:57:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python2.7相当陌生,但我正试图从一个网站上获得一个简单的段落,但python输出[]。我设法提取了数字,但没有提取文本

任何帮助都会很好,谢谢

import urllib
import re

HTML_File = urllib.urlopen("http://uk.weather.com/weather/10day/New+Romney+KEN+United+Kingdom+UKXX1121:1:UK")
HTML_Text = HTML_File.read()

LastUpdate_Pattern = re.compile('<div class="wx-24hour-title"> <h2>New Romney 10-Day Forecast</h2> <p class="wx-timestamp"> (.*?) </p>')

LastUpdate = re.findall(LastUpdate_Pattern, HTML_Text)

print LastUpdate

Tags: textimportrenewhtmlh2urllibclass
1条回答
网友
1楼 · 发布于 2024-04-19 18:57:49

使用BeautifulSoup

import urllib
from bs4 import BeautifulSoup

HTML_File = urllib.urlopen("http://uk.weather.com/weather/10day/New+Romney+KEN+United+Kingdom+UKXX1121:1:UK")
HTML_Text = HTML_File.read()
soup = BeautifulSoup(HTML_Text, 'html.parser')

print soup.select('.wx-timestamp')[0].text

输出:

Updated:

last updated about 20 minutes ago

相关问题 更多 >