java抓取网页数据(我正在使用python、pandas和BeautifulSoup创建一个网络抓取程序 )
优采云 发布时间: 2021-11-13 06:10java抓取网页数据(我正在使用python、pandas和BeautifulSoup创建一个网络抓取程序
)
我正在使用 python、pandas 和 BeautifulSoup 创建一个网络抓取程序。我希望它每 10 分钟向气象站请求一次风信息。此数据将存储在收录 72 个索引(24 小时)的数组中。
到目前为止,我已经设法用当前条件创建了一个凌乱的数据框。我有3个问题,第三个问题此时可能超出了我的能力范围。
1:解析时如何从我的数据中排除'/n'?
2:如何每10分钟更新一次并添加到数组中
3:如何从数组中push/pop数据,并在数组前面显示最近的数据。(我已经阅读过 push 和 pop,这可能是我将来可以研究的内容。)
这是我自己写的代码的第一部分,所以请原谅我。我在下面插入了我的代码,这是一个显示我的输出的图像链接。
import pandas as pd
import requests
from bs4 import BeautifulSoup
url = 'https://www.wunderground.com/weather/us/ca/montara'
page = requests.get(url)
page.text
soup = BeautifulSoup(page.text, 'html.parser')
#read wind speed
montara_wSpeed = []
montara_wSpeed_elem = soup.find_all(class_='wind-speed')
for item in montara_wSpeed_elem:
montara_wSpeed.append(item.text)
#read wind direction
montara_wCompass = []
montara_wCompass_elem = soup.find_all(class_='wind-compass')
for item in montara_wCompass_elem:
montara_wCompass.append(item.text)
#read wind station
montara_station = []
montara_station_elem = soup.find_all(class_='station-nav')
for item in montara_station_elem:
montara_station.append(item.text)
#create dataframe
montara_array = []
for station, windCompass, windSpeed in zip(montara_station, montara_wCompass, montara_wSpeed):
montara_array.append({'Station': station, 'Wind Direction': windCompass, 'Wind Speed': windSpeed})
df = pd.DataFrame(montara_array)
df