爬取“新浪财经-股票-行情-北交所”的全部5页共200支股票数据,并将爬取的200条股票数据写入“csv文件操作步骤
时间: 2024-03-20 11:43:56 浏览: 72
好的,以下是操作步骤:
1. 导入所需库
```
import requests
from bs4 import BeautifulSoup
import pandas as pd
```
2. 构造请求头和请求参数
```
url = "https://finance.sina.com.cn/stock/north.shtml"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"}
params = {
"page": 1
}
```
3. 爬取数据并解析
```
data_list = []
for i in range(1, 6): # 爬取5页数据
params["page"] = i
response = requests.get(url, headers=headers, params=params)
soup = BeautifulSoup(response.text, "html.parser")
table = soup.find("table", class_="table_list")
tr_list = table.find_all("tr")[1:] # 去掉表头行
for tr in tr_list:
td_list = tr.find_all("td")
stock_code = td_list[0].text.strip()
stock_name = td_list[1].text.strip()
latest_price = td_list[2].text.strip()
change_amount = td_list[3].text.strip()
change_rate = td_list[4].text.strip()
volume = td_list[5].text.strip()
amount = td_list[6].text.strip()
data_list.append([stock_code, stock_name, latest_price, change_amount, change_rate, volume, amount])
```
4. 将数据写入csv文件
```
df = pd.DataFrame(data_list, columns=["股票代码", "股票名称", "最新价", "涨跌额", "涨跌幅", "成交量", "成交额"])
df.to_csv("north_stocks.csv", index=False, encoding="utf-8")
```
以上就是完整的操作步骤,您只需要运行这段代码即可在当前目录下生成一个名为“north_stocks.csv”的文件,其中包含200条股票数据。
阅读全文