from urllib import request from bs4 import BeautifulSoup as bs import re
defgetCodes(): url = 'http://quote.eastmoney.com/stocklist.html'; resp = request.urlopen(url) resp_text = resp.read().decode('gbk') soap = bs(resp_text, 'html.parser') list = soap.find_all('div', id = 'quotesearch')[0].find_all('ul')[0].find_all('li')
codeList = [] for li in list: try: #eg: sh603183 codeList.append(re.findall(r"[s][hz]\d{6}", li.find('a')['href'])) except: continue return codeList
defmakeDict(code): infoDict = {} url = 'https://gupiao.baidu.com/stock/'+ code +'.html'; resp = request.urlopen(url) resp_text = resp.read().decode('utf-8') soap = bs(resp_text, 'html.parser') try: stockInfo = soap.find_all(attrs = {'class','stock-bets'}) name = soap.find(attrs = {'class', 'bets-name'}) if name isNone: returnNone infoDict['name'] = name.text.strip() keys = soap.find_all('dt') values = soap.find_all('dd') for i in range(len(keys)): infoDict[keys[i].text] = values[i].text
return infoDict except: returnNone
defwriteFile(codeList): i=0 for code in codeList: i = i+1 # 下面两个判断是因为前45个股票百度并没有信息,所以跳过了,200个以后的数据就不再取了,太多了,科科 if i < 45: continue if i > 200: break infoDict = makeDict(code[0]) if infoDict isNone: continue print (infoDict) with open('E://Data.txt', 'a', encoding = 'utf-8') as f: f.write(str(infoDict) + '\n')