import csv
import requests
from bs4 import BeautifulSoup
# create initial CSV file
with open('mycsvfile.csv','w') as f:
f.write('Period,Short_Desc,Temp\n') # TRAILING NEWLINE
# Call Website to get data
page = requests.get("http://forecast.weather.gov/MapClick.php?lat=37.7772&lon=-122.4168")
soup = BeautifulSoup(page.content, 'html.parser')
location = soup.find(id="fcst-search")
location_items = location.find_all(id="getfcst-headOffice")
location_name = location_items[0]
# stuck here? confused...
location_site = location_name.find(class_="period-name").get_text()
print(location_name.prettify())
如果你查看链接,我试图得到城市的名称,然后在我的工作代码中输出到csv。如何在href标签中获取城市名称?感谢。
<div id="getfcst-head">
<p>Your local forecast office is</p>
<h3 id="getfcst-headOffice"><a href="http://www.wrh.noaa.gov/sew">Seattle, WA</a></h3>
</div>
答案 0 :(得分:0)
HTML没有显示源代码中的链接...至少不能通过BeautifulSoup显示。那个“本地预测办公室”正在动态地从其他地方撤出。您可以从页面上的其他位置拉出来......
import csv
import requests
from bs4 import BeautifulSoup
# create initial CSV file
with open('mycsvfile.csv','w') as f:
f.write('Period,Short_Desc,Temp\n') # TRAILING NEWLINE
# Call Website to get data
page = requests.get("http://forecast.weather.gov/MapClick.php?lat=37.7772&lon=-122.4168")
soup = BeautifulSoup(page.content, 'html.parser')
location_header = soup.find('a', id="localWFO").get('title')
print location_header
结果:
San Francisco Bay Area/Monterey, CA