【发布时间】:2018-08-21 12:50:02
【问题描述】:
我正在尝试使用 beautifulsoup 4 从 wunderground.com 抓取一些天气数据。 我能够找到有关如何执行此操作的教程,但是它显示了如何使用 HTML 源代码来执行此操作。 Wunderground.com 在制作教程时曾经是 HTML 格式,但现在是 js 格式。
我能够获取代码并根据我的特定数据检索需求对其进行操作,但我一直坚持如何让它拉动 javascript 而不是 HTML。有人可以帮忙吗?
代码如下,我从 youtube 上 SAS Business Analytics 的 kiengiv 获取。
from bs4 import BeautifulSoup
import urllib3, csv, os, datetime, urllib3.request, re, sys
for vYear in range(2016, 2019):
for vMonth in range(1, 13):
for vDay in range(1, 32):
# go to the next month, if it is a leap year and greater than the 29th or if it is not a leap year
# and greater than the 28th
if vYear % 4 == 0:
if vMonth == 2 and vDay > 29:
break
else:
if vMonth == 2 and vDay > 28:
break
# go to the next month, if it is april, june, september or november and greater than the 30th
if vMonth in [4, 6, 9, 11] and vDay > 30:
break
# defining the date string to export and go to the next day using the url
theDate = str(vYear) + "/" + str(vMonth) + "/" + str(vDay)
# the new url created after each day
theurl = "https://www.wunderground.com/history/daily/us/ma/cambridge/KBOS/" + theDate + "date.html"
# extract the source data for analysis
http = urllib3.PoolManager()
thepage = http.request('GET', theurl)
soup = BeautifulSoup(thepage, "html.parser")
MaxWindSpeed = Visibility = SeaLevelPressure = Precipitation = High_Temp = Low_Temp = Day_Average_Temp = "N/A"
for temp in soup.find_all('tr'):
if temp.text.strip().replace('\n', '')[:6] == 'Actual' or temp.text.strip().replace('\n', '')[-6:] == "Record":
pass
elif temp.text.replace('\n', '')[-7:] == "RiseSet":
break
elif temp.find_all('td')[0].text == "Day Average Temp":
if temp.find_all('td')[1].text.strip() == "-":
Mean = "N/A"
else:
Mean = temp.find_all('td')[1].find(attrs={"<td _ngcontent-c7" : "</td>"}).text
elif temp.find_all('td')[0].text == "High Temp":
if temp.find_all('td')[1].text.strip() == "-":
Max = "N/A"
else:
Max = temp.find_all('td')[1].find(attrs={"<td _ngcontent-c7" : "</td>"}).text
elif temp.find_all('td')[0].text == "Low Temp":
if temp.find_all('td')[1].text.strip() == "-":
Min = "N/A"
else:
Min = temp.find_all('td')[1].find(attrs={"<td _ngcontent-c7" : "</td>"}).text
elif temp.find_all('td')[0].text == "Growing Degree Days":
if temp.find_all('td')[1].text.strip() == "-":
GrowingDegreeDays = "N/A"
else:
GrowingDegreeDays = temp.find_all('td')[1].text
elif temp.find_all('td')[0].text == "Heating Degree Days":
if temp.find_all('td')[1].text.strip() == "-":
HeatingDegreeDays = "N/A"
else:
HeatingDegreeDays = temp.find_all('td')[1].text
elif temp.find_all('td')[0].text == "Dew Point":
if temp.find_all('td')[1].text.strip() == "-" or temp.find_all('td')[1].text.strip() == "":
DewPoint = "N/A"
else:
DewPoint = temp.find_all('td')[1].find(attrs={"<td _ngcontent-c7" : "</td>"}).text
elif temp.find_all('td')[0].text == "Precipitation" and temp.find_all('td')[1].text.strip() != "":
if temp.find_all('td')[1].text.strip() == "-" or temp.find_all('td')[1].text.strip() == "":
Precipitation = "N/A"
else:
Precipitation = temp.find_all('td')[1].find(attrs={"<td _ngcontent-c7" : "</td>"}).text
elif temp.find_all('td')[0].text == "Sea Level Pressure" and temp.find_all('td')[1].text.strip() != "":
if temp.find_all('td')[1].text.strip() == "-":
SeaLevelPressure = "N/A"
else:
SeaLevelPressure = temp.find_all('td')[1].find(attrs={"<td _ngcontent-c7" : "</td>"}).text
elif temp.find_all('td')[0].text == "Max Wind Speed":
if temp.find_all('td')[1].text.strip() == "-" or temp.find_all('td')[1].text.strip() == "":
MaxWindSpeed = "N/A"
else:
MaxWindSpeed = temp.find_all('td')[1].find(attrs={"<td _ngcontent-c7" : "</td>"}).text
elif temp.find_all('td')[0].text == "Visibility":
if temp.find_all('td')[1].text.strip() == "-":
Visibility = "N/A"
else:
Visibility = temp.find_all('td')[1].find(attrs={"<td _ngcontent-c7" : "</td>"}).text
break
# combining the values to be written to the CSV file
CombinedString = theDate + "," + Mean + "," + Max + "," + Min + "," + HeatingDegreeDays + "," + DewPoint + "," + "," + Precipitation + "," + SeaLevelPressure + "," + MaxWindSpeed + "," + Visibility + "," + Events + "\n"
file.write(bytes(CombinedString, encoding="ascii", errors='ignore'))
# printing to help with any debugging and tracking progress
print(CombinedString)
file.close()
【问题讨论】:
-
嗨,大部分代码是不可读的。你需要什么信息?我在开发者控制台中找到了带有数据的 jsons
-
嗨,我有源代码,但我只是不知道如何正确地从 js 而不是 HTML 中抓取它。我使用的原始代码是为抓取 HTML 而构建的,所以我想知道是否有办法转换它。
-
作为旁注,您能否附上您在代码中看到的内容?它对我来说似乎是可读的......
-
我认为你不需要废弃 html。我认为获取正确的 json 就足够了,但是如果我不知道您要获取的确切数据,我无法帮助您^^
标签: javascript python html web-scraping beautifulsoup