import requests
from bs4 import BeautifulSoup
from urllib import request
import re
import pymongo
mongo_client = pymongo.MongoClient('mongodb://localhost:27017/')
mongo_db = mongo_client['quant_01']
mongo_collection = mongo_db['EPS_Stock']
'''
Company name:', 'Net assets per share:', 'Earnings per share:', 'Net profit:', 'Net profit growth rate:',
'Net profit growth rate:', 'Operating income:', ' Total share capital: ', 'tradable shares:
'''
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3877.400 QQBrowser/10.8.4507.400'
}
all_list = []
for i in range(3):
Furl = 'http://stockpage.10jqka.com.cn/'
allrul = ['600511', '600512', '600513', '600515']
url = Furl + allrul[i]
print(url)
req = request.Request(url=url, headers=headers)
rsp = request.urlopen(req)
html = rsp.read().decode('utf-8')
bf = BeautifulSoup(html, "html.parser")
texts = bf.find_all('dl', class_='company_details')
str_Result = str(texts)
splitR = str_Result.splitlines()
print(splitR)
print(len(splitR))
result = re.findall(r"<dd.+?/dd>|<dt.+?/dt>", str_Result, flags=0)
num_list1 = []
num_list2 = []
for i in range(len(result)):
if result[i].find('公司名称') > -1:
num_list1.append(result[i])
num_list2.append(result[i + 1])
else:
pass
if result[i].find('上市时间') > -1:
num_list1.append(result[i])
num_list2.append(result[i + 1])
else:
pass
if result[i].find('每股净资产') > -1:
num_list1.append(result[i])
num_list2.append(result[i + 1])
else:
pass
if result[i].find('每股收益') > -1:
num_list1.append(result[i])
num_list2.append(result[i + 1])
else:
pass
if result[i].find('净利润') > -1:
num_list1.append(result[i])
num_list2.append(result[i + 1])
else:
pass
if result[i].find('净利润增长率') > -1:
num_list1.append(result[i])
num_list2.append(result[i + 1])
else:
pass
if result[i].find('营业收入') > -1:
num_list1.append(result[i])
num_list2.append(result[i + 1])
else:
pass
if result[i].find('总股本') > -1:
num_list1.append(result[i])
num_list2.append(result[i + 1])
else:
pass
if result[i].find('流通股') > -1:
num_list1.append(result[i])
num_list2.append(result[i + 1])
else:
pass
print('$$$$$$$$$$$$$$$$$$$$$$$$$')
Important_Books1 = ['Company_Name', 'Net_assets_per_share', 'EPS', 'Net_Profit', 'Net_Profit_Ratio', 'operating_revenue',
'total_share_capital', 'outstanding_shares']
Important_Books2 = []
for m in num_list2:
Important_Books2.append(m[4:-5])
print(Important_Books2)
dict1 = dict(zip(Important_Books1, Important_Books2))
all_list.append(dict1)
''' 列表转字典
list1 = ['key1','key2','key3']
list2 = ['1','2','3']
dict(zip(list1,list2))
{'key1':'1','key2':'2','key3':'3'}
'''
print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&')
print(all_list)