Python爬虫爬取东方财富网的股票信息

目标:爬取东方财富网的股票信息

提醒:在此之前你需要一些Python基础知识;
我们开始吧!
首先是在这个爬虫中会用到的一些库
1.request库和selenium库(获取网页信息)
2.BeautifulSoup库(对网页信息进行处理)
3.matplotlib库(对爬取信息进行分析)
4.time库

1.先找到要爬取的网页

Python爬虫爬取东方财富网的股票信息
这里要爬取股票信息有两个方法:
1.直接通过http://so.eastmoney.com/web/s?keyword=股票名称
这个URL访问
2.通过http://quote.eastmoney.com/stocklist.html获取所有股票名称和URL
如图:Python爬虫爬取东方财富网的股票信息
我这里是使用的第二种方法,因为一开始我想先获取所有股票的URL连接然后
全部爬取下来做分析。

2.第二步开始爬取信息

def getHtml(url):
    requests.get(url)
    soup = BeautifulSoup(r.text,"html.parser")
    return soup

2.1 通过requests获取网页信息

def findlist():
    url="http://quote.eastmoney.com/stocklist.html"
    soup= getHtml(url)
    search= soup.find("div",{"id":"quotesearch"})
    ullist=search.find_all("ul")
    for ul in ullist:
        lilist= ul.find_all("li")
        for li in lilist:
            data = []
            a= li.find("a",{"target":"_blank"})
            data.append(a.string)
            data.append(a.attrs["href"])
            stocklist.append(data)

2.2再通过BeautifulSoup解析;

本来我是想要通过获取网页的请求,来看看到底数据是怎么来的,有可能是JSON什么的,也许就不用怎么麻烦;后来…
Python爬虫爬取东方财富网的股票信息
数据都在HTML里面,那就难受了。只能老老实实的解析HTML了。

2.3分析股票详情页面

进入股票详情页面;看到啦,要爬取的数据就在这里
Python爬虫爬取东方财富网的股票信息
通过前面的方法继续获取网页信息,然后就遇到了一个问题
Python爬虫爬取东方财富网的股票信息
这个HTML里面竟然没有数据
后来查了一下发现这个网页的数据是通过JS动态加载的并没有在HTML里面
但是我们可以通过获取这个网页请求数据的链接来获取我们想要的数据
然后我就找啊找终于找到了
Python爬虫爬取东方财富网的股票信息
这就是我们找的数据
看一下它的URL
Python爬虫爬取东方财富网的股票信息
可以看见这个请求中有很多参数,先分析一下。cmd是股票代码,最后一个是时间戳加了三个数字
我试了一下好像获取不到数据,一直获取的都是第一次数据
肯定是参数没有对

不管了,然后我就换了一个方法用selenium模拟浏览器可以获取加载完成的HTML

from selenium import webdriver
import time

driver = webdriver.Chrome()

def getHtml(url):
    driver.get(url)
    time.sleep(3)
    soup = BeautifulSoup(driver.page_source,"html.parser")
    return soup

注意:在使用selenium的时候最好加个延迟,不然很可能获取不到数据
然后其他步骤和前面一样最后得到数据

def finddata(soup):
    singledata = {}
    datadiv=soup.find("div",{"class":"qphox layout mb7 clearfix"})
    if(datadiv==None):
        datadiv=soup.find("div",{"class":"qphox layout mb7"})
        if(datadiv!=None):
            singledata= finddata2(datadiv,singledata)
        return singledata
    strong=datadiv.find("div",{"id":"arrowud"}).strong
    singledata["color"]=strong.attrs["class"][-1]
    singledata["number"]=strong.string
    ullist=datadiv.find_all("ul")
    for ul in ullist:
        lilist=ul.find_all("li")
        for li in lilist:
            singledata[li.span.string]=li.span.nextSibling.string
    return singledata

def finddata2(datadiv,singledata):
    strong=datadiv.find("strong",{"id":"price9"})
    singledata["color"] = strong.attrs["class"][-1]
    singledata["number"] = strong.string
    trlist=datadiv.find_all("tr")
    i=1
    for tr in trlist:
        tdlist=tr.find_all("td")
        for td in tdlist:
            if(td.attrs=={}):
                singledata[td.string]=tr.find("td",{"id":"gt"+str(i)}).string
                i = i + 1
    return singledata

Python爬虫爬取东方财富网的股票信息

3.数据分析

通过matplotlib库可以对获取的数据进行可视化分析

import matplotlib.pyplot as plt
numlist=[1,2,3,4,5]
plt.plot(numlist, 'k')
plt.show()

不过由于我用的是selenium所以爬取速度有点慢,数据量不够大,爬了一个小时才勉强能用
Python爬虫爬取东方财富网的股票信息
源码在此https://github.com/YOURENTUTU/python/blob/master/