Python noob here:这可以很好地从URL中提取文本,但我不理解它引发的无效语法错误:
>>> from bs4 import BeautifulSoup
>>> with open("https://www.nhc.noaa.gov/xml/TWOAT.xml") as markup:
... soup = BeautifulSoup(markup.read())
... text = soup.get_text()
File "<stdin>", line 3
text = soup.get_text()
^
SyntaxError: invalid syntax
>>> print(text)
它给出了这个输出,正是我想要的:
Atlantic Tropical Weather Outlook
000
ABNT20 KNHC 081908
TWOAT
Tropical Weather Outlook...Retransmitted
NWS National Hurricane Center Miami FL
200 PM EDT Thu Aug 8 2019
For the North Atlantic...Caribbean Sea and the Gulf of Mexico:
Tropical cyclone formation is not expected during the next 5 days.
$$
Forecaster Pasch
>>>
您得到的错误是因为在
soup = BeautifulSoup(markup.read())
part之后没有按两次Enter,python认为您仍然处于缩进之下。但即使这样做,也会出现另一个错误,因为您试图将远程位置作为文件打开。这行不通尝试使用请求获取数据:
相关问题 更多 >
编程相关推荐