1 回答

TA貢獻(xiàn)1824條經(jīng)驗 獲得超8個贊
使用此代碼:
from bs4 import BeautifulSoup
import urllib.request, urllib.parse, urllib.error
html_url = 'https://www.nwk.usace.army.mil/Locations/District-Lakes/Pomme-de-Terre-Lake/Daily-Lake-Info-2/'
html_doc = urllib.request.urlopen(html_url).read()
soup = BeautifulSoup(html_doc, 'html.parser')
pageNav = soup.find(class_= 'Normal')
pageSub = pageNav.find_all('p')
for strong_tag in soup.find_all('strong'):
if strong_tag.text == "24 Hr. Change:" or strong_tag.text=="Yesterday's High:" or strong_tag.text=="Date: " or strong_tag.text=="Lake Surface Temperature:":
print(strong_tag.text, strong_tag.next_sibling)
if 語句應(yīng)該對所有內(nèi)容進行排序。我在 jupyter notebook 中嘗試了這段代碼,它奏效了。這里唯一的問題是日期一詞后面有一些空格。所以現(xiàn)在文件不會打印日期行。
要對日期大小寫進行硬編碼,請改用以下代碼:
from bs4 import BeautifulSoup
import urllib.request, urllib.parse, urllib.error
html_url = 'https://www.nwk.usace.army.mil/Locations/District-Lakes/Pomme-de-Terre-Lake/Daily-Lake-Info-2/'
html_doc = urllib.request.urlopen(html_url).read()
soup = BeautifulSoup(html_doc, 'html.parser')
pageNav = soup.find(class_= 'Normal')
pageSub = pageNav.find_all('p')
date = True
for strong_tag in soup.find_all('strong'):
if date:
print(strong_tag.text, strong_tag.next_sibling)
date = False
if strong_tag.text == "24 Hr. Change:" or strong_tag.text=="Yesterday's High:" or strong_tag.text=="Lake Surface Temperature:":
print(strong_tag.text, strong_tag.next_sibling)
添加回答
舉報