Python Demo
import numpy as np
import pandas as pd
from pandas_datareader import data, wb
import requests
from bs4 import BeautifulSoup
# http://quote.eastmoney.com/sz002026.html
header = {
'Connection': 'Keep-Alive',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0)'
}
# fund_url = 'http://fund.eastmoney.com/f10/000001.html'
share_url = 'http://quote.eastmoney.com/sz002026.html'
res = requests.get(share_url, headers=header, timeout=10)
# ISO-8859-1
# var = res.encoding
# print(var)
# re = res.text.encode("gbk")
# print(res.text)
res.encoding = 'gbk'
f = open(r'C:\Users\want\Desktop\sz002026.html', 'w', encoding='gbk')
f.write(res.text)
f.close()
soup = BeautifulSoup(res.text, 'html.parser')
# print(soup)
# fund_name = soup.html.head.title.string
#
# print(fund_name)
share_name = soup.find('h2', id="name").string
share_code = soup.find('b', id="code").string
print(share_name)
print(share_code)
jk = soup.find('td', id="gt5").string
print(jk)
本文介绍了一个使用Python爬取东方财富网股票信息的例子。通过发送HTTP请求并解析返回的HTML页面,可以获取特定股票的名称和代码等基本信息。
1490

被折叠的 条评论
为什么被折叠?



