Python把漂亮的汤输给一只小Python

2024-09-25 00:21:34 发布

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

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
>>> 


Tags: thetexturlgetherexmlweatheroutlook
1条回答
网友
1楼 · 发布于 2024-09-25 00:21:34

您得到的错误是因为在soup = BeautifulSoup(markup.read())part之后没有按两次Enter,python认为您仍然处于缩进之下。但即使这样做,也会出现另一个错误,因为您试图将远程位置作为文件打开。这行不通

尝试使用请求获取数据:

import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.nhc.noaa.gov/xml/TWOAT.xml')
soup = BeautifulSoup(r.text)
text = soup.get_text()
print(text)

相关问题 更多 >