地震数据爬取访问“http://www.ceic.ac.cn/speedsearch”网站,编写程序,爬取并输出最近24小时内的地震数据。
时间: 2024-01-17 16:03:42 浏览: 95
好的,我可以帮你编写一个Python程序来实现这个功能。你需要安装Requests和BeautifulSoup库来解析网页。以下是完整的代码:
```python
import requests
from bs4 import BeautifulSoup
url = "http://www.ceic.ac.cn/speedsearch"
data = {"start": "1", "end": "10", "timetype": "4", "nodetype": "1,4", "minmag": "", "maxmag": "",
"mindepth": "", "maxdepth": "", "minlat": "", "maxlat": "", "minlon": "", "maxlon": "",
"cataid": "", "src": "", "orderby": "TIME", "zm": "8"}
response = requests.post(url, data=data)
soup = BeautifulSoup(response.text, "html.parser")
table = soup.find_all('table', class_='tb')[0]
rows = table.find_all('tr')
for row in rows[1:]:
cols = row.find_all('td')
time = cols[0].get_text()
lat = cols[2].get_text()
lon = cols[3].get_text()
depth = cols[4].get_text()
magnitude = cols[5].get_text()
location = cols[6].get_text()
print(f"时间:{time},纬度:{lat},经度:{lon},深度:{depth},震级:{magnitude},地点:{location}")
```
该程序使用POST请求向目标网站发送数据,获取最近24小时内的地震数据,并使用BeautifulSoup库解析HTML页面。程序会输出地震的时间、经纬度、深度、震级和地点。你可以根据需要对代码进行修改和优化。
阅读全文