鱼C论坛

 找回密码
 立即注册
查看: 2078|回复: 2

[技术交流] python 爬虫获取股票信息

[复制链接]
发表于 2018-3-16 16:32:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 结果咧 于 2018-3-16 17:05 编辑

import requests
import re
from bs4 import BeautifulSoup

def get_htmltext(url):#获取网页代码,两处网站用到
    headers={'user-agent':'Mozilla/5.0'}
    response=requests.get(url,headers=headers)
    response.encoding=response.apparent_encoding
    html=response.text
    return html

def get_stocklist(html):#从东方财富网信息中提取股票列表
    lis=re.findall(r'<li><a target="_blank" href="http://q.*?</a></li>',html)
    for li in lis:
        string=li[71:li.find('</a>')]
        string_name=string.split('(')[0]
        #string_code=string.split('(')[1].strip(')')
        string_code=li[56:li.find('.html">')]
        stockname_list.append(string_name)
        stockcode_list.append(string_code)

def get_url(stockcode_list):#由东方财富网股票列表获得百度股票的url地址列表
    for stockcode in stockcode_list:
        url='https://gupiao.baidu.com/stock/'+stockcode+'.html'
        url_list.append(url)

def get_stockinfo(html):#注意处理无法打开的股票
    soup=BeautifulSoup(html,'html.parser')
    strong=soup.find_all('strong')
    if len(strong)==0:#空列表,没有找到
        stockinfo_list.append(['无','无','无'])
    else:
        spans=strong[0].find_next_siblings('span')
        stockinfo_list.append([strong[0].string,spans[0].string,spans[1].string])
   
def get_file(stockname_list,stockcode_list,stockinfo_list):
    f=open('上交所深交所股票信息.txt','w')
    f.write('名字'+'\t'+'代码'+'\t'+'信息'+'\n')
    for name,code,info in zip(stockname_list,stockcode_list,stockinfo_list):
        f.write(name+'\t')
        f.write(code+'\t')
        f.write(str(info)+'\n')
    f.close()

if __name__=='__main__':
    stockname_list=[]
    stockcode_list=[]
    stockinfo_list=[]#股票名字,编号与股票信息列表
    url_list=[]#网页链接列表
   
    url_old='http://quote.eastmoney.com/stocklist.html'#东方财富网url
    html_old=get_htmltext(url_old)#东方财富网股票源代码信息
    get_stocklist(html_old)#获取股票编号列表
    get_url(stockcode_list)#获取url列表
   
    for url in url_list:#注意:有些股在百度股票中是打不开的,这里很坑,这里可以设置数量
        html=get_htmltext(url)#获取百度股票网页源代码
        get_stockinfo(html)#获取股票的信息列表
  
    get_file(stockname_list,stockcode_list,stockinfo_list)



新人求罩
   

可是出现
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='gupiao.baidu.com', port=443): Max retries exceeded with url: /stock/sh600587.html (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000000001CC47710>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',))
这种问题怎么破?????????????????????
        
        
        
        
   
   
   

这是爬取结果……

这是爬取结果……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-16 16:45:12 | 显示全部楼层
膜拜大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-16 17:04:50 | 显示全部楼层

离大佬还差的远
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 20:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表