股票信息爬取

# -*- coding: utf-8 -*-
"""
Created on Sat Apr  7 16:39:18 2018

@author: Administrator
"""

import requests
import re
from bs4 import BeautifulSoup

headers={
        'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
        }
ll=[]
bb=[]
def geturl(url):
    res=requests.get(url,headers=headers)
    res.encoding='gb2312'
    soup=BeautifulSoup(res.text,'html.parser')
    lianjie=soup.find_all('a')
    for hh in lianjie:
        try:
            ll.append(hh['href'])
        except:
            pass
    for lian in ll:
        try:
            bianhao=re.findall(r'[s][hz]\d{6}',lian)[0]
            if len(bianhao) != 0:
                bb.append(bianhao)
            else:
                pass
        except:
            pass
    return bb

info=[]
def getinfo(URL):
    r=requests.get(URL,headers=headers)
    r.encoding='utf-8'
    s=BeautifulSoup(r.text,'html.parser')
    all=s.find_all('div',class_='stock-bets') 
    dict={}
    for each in all:
        title=each.find('a',class_='bets-name').text.split()[0].strip()
        dt=each.find_all('dt')
        dtt=list(map(lambda x:x.text,dt))
        dd=each.find_all('dd')
        ddd=list(map(lambda x:x.text.strip(),dd))
        dict.update({'股票名称':title})
        for i in range(len(dtt)):
            key=dtt[i]
            val=ddd[i]
            dict[key]=val
    info.append(dict)
    return info

if __name__ == '__main__':
    url='http://quote.eastmoney.com/stocklist.html'
    html=geturl(url)
    URL=['https://gupiao.baidu.com/stock/'+str(bh)+'.html' for bh in html]
    for uu in URL[100:111]:
        getinfo(uu)
    print(info)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值