Article | carefree huan \

Source: Python technology “ID: PYTHonAll”

Because of the epidemic, the whole world is printing money at an accelerated pace. In this “flood” context, the value of our cash on hand is accelerating. If we can outperform inflation through financial management, it is a question for each of us to think about. There are few investment channels in China, and most people choose bank term or various kinds of treasure. However, it is obvious that both the interest rate of bank term and various kinds of treasure are falling, and even if the inflation level remains unchanged, they can’t win.

So some people turn to the fund and securities market. Buying fund is to choose fund manager, believe he can help you make money, buying a stock is to choose a company, believe oneself eye. As a young man in the new era, I am still willing to choose the company by myself, use my technology to assist decision-making and build my own trading system. This article introduces how to obtain the securities company’s analyst research report to assist us in stock selection.

Some time ago, I used this research method to find a stock, which was ALLIN at that time. With good luck, the profit exceeded 100,000 in less than a month, so I sold it happily. Of course, there is an element of luck, but this method can improve the efficiency of stock selection, to assist their decision-making.

Finding the target page

We need to use the analyst’s report to assist us in stock selection. The first step is to obtain the analyst’s report. My first reaction is to go to the Oriental Wealth website (www.eastmoney.com/) to look for it.

We went to the home page of Oriental Fortune and found the “data” subcolumn of the “stocks” column:

Stocks – Data

Click to go to the front page of stock data, then we find the left menu, find “research report”, select the sub-menu of research report “Stock research report” :

Research report – Stock research report

Click in, is our single stock research page, this is the target page for us to obtain research:

Stocks research report

On this page, we can see all the research reports of individual stocks in the longest two years. We can see the research reports, relevant individual stocks, stock ratings, stock future earnings forecast and other information, which is the key for us to select stocks and the information we need to obtain.

To get the data

Looking at this page, we can see that this is a paginated table, and we may need to paginate to get the data. Anyway, let’s go to developer tools, refresh the page, and see if we can find the data request. Since the home page was preloaded, we didn’t find any data requests, so we tried to click on the second page to see:

The second page

We can easily find the data request, and the request we see looks like this:

Reportapi.eastmoney.com/report/list… &pageSize=50&industry=&rating=&ratingChange=&beginTime=2018-10-27&endTime=2020-10-27&pageNo=2&fields=&qType=0&orgCode=&c ode=*&rcode=&p=2&pageNum=2&_=1603766081350

Let’s take a look at the data returned by this request:

The second page returns data

Obviously, this is the data we need, and it’s so easy to get to you! Our next task is to analyze the REQUEST URL and parameters.

The URL is also easy to analyze, and we can clearly see several key parameters:

  • PageSize: Indicates the number of records per page
  • BeginTime: Indicates the start time
  • EndTime: indicates the end date
  • PageNo: the page number
  • PageNum: the page number

These parameters are easy to observe. We can try this a few times with different pages and see that pageNum is useless. The pageNo parameter is fine for pages. In addition, we can see that the returned result is wrapped around the JSON data, starting with “datatable3175804”, which is actually the CB parameter inside the parameter. The other parameters are conditional filters, which can be copied if you don’t want to filter for certain conditions.

With this information, we can start writing a program to request the data:


    def __init__(self):
        self.header = {"Connection""keep-alive"."Cookie""st_si=30608909553535; cowminicookie=true; st_asi=delete; cowCookie=true; intellpositionL=2048px; qgqp_b_id=c941d206e54fae32beffafbef56cc4c0; st_pvi=19950313383421; st_sp=2020-10-19%2020%3A19%3A47; st_inirUrl=http%3A%2F%2Fdata.eastmoney.com%2Fstock%2Flhb.html; st_sn=15; st_psi=20201026225423471-113300303752-5813912186; intellpositionT=2579px"."User-Agent""Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari."Host""reportapi.eastmoney.com"
                          }

        self.conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='east_money', charset='utf8')
        self.cur = self.conn.cursor()
        self.url = 'http://reportapi.eastmoney.com/report/list?cb=datatable1351846&industryCode=*&pageSize={}&industry=*&rating=&ratingChan ge=&beginTime={}&endTime={}&pageNo={}&fields=&qType=0&orgCode=&code=*&rcode=&p=2&pageNum=2&_=1603724062679'

    def getHtml(self, pageSize, beginTime, endTime, pageNo):
        print(self.url.format(pageSize, beginTime, endTime, pageNo))
        response = requests.get(self.url.format(pageSize, beginTime, endTime, pageNo), headers=self.header)
        html = response.content.decode("utf-8")

        return html
Copy the code

Note that the returned data is encoded in UTF-8 and needs to be decoded in UTF-8.

After obtaining the data, the next step is to parse the data and extract the important data we feel we need from the returned data:


def format_content(self, content):
        if len(content):
            content = content.replace('datatable1351846('.' ')[:- 1]
            return json.loads(content)
        else:
            return None


    def parse_data(self, items):
        result_list = []
        for i in items['data']:
            result = {}
            obj = i
            result['title'] = obj['title'] # Report name result['stockName'] = obj['stockName'] # stock name result['stockCode'] = obj['stockCode'] # stock code result['orgCode'] = obj['stockCode'] # organization code result['orgName'] = obj['orgName'] # name of organization result['orgSName'] = obj['orgSName'] # organization abbreviation result['publishDate'] = obj['publishDate'] # release date result['predictNextTwoYearEps'] = obj['predictNextTwoYearEps'] # Annual earnings per share result['predictNextTwoYearPe'] = obj['predictNextTwoYearPe'] # P/E ratio for next year'predictNextYearEps'] = obj['predictNextYearEps'] # Next year earnings per share result['predictNextYearPe'] = obj['predictNextYearPe'] # Next year p/E ratio result['predictThisYearEps'] = obj['predictThisYearEps'] # Earnings per share result['predictThisYearPe'] = obj['predictThisYearPe'] # P/E ratio result['indvInduCode'] = obj['indvInduCode'] # industry code result['indvInduName'] = obj['indvInduName'] # industry name result['lastEmRatingName'] = obj['lastEmRatingName'] # Name of last rating result['lastEmRatingValue'] = obj['lastEmRatingValue'] # last rating code result['emRatingValue'] = obj['emRatingValue'] # rating code result['emRatingName'] = obj['emRatingName'] # rating name result['ratingChange'] = obj['ratingChange'] # rating change result['researcher'] = obj['researcher'] # researcher result['encodeUrl'] = obj['encodeUrl'] # link result['count'] = int(obj['count']) # Recent monthly stock research result_list.append(result)

        return result_list
Copy the code

Here we unwrap the returned data, parse it into JSON, and store it in a list.

After we get the data, the next step is to store it. Considering that we will use this data frequently in the future and will constantly obtain the latest data, I need to store it in a medium that is convenient for query and stable performance. Here, I use MySQL for storage.


def insertdb(self, data_list):
        attrs = ['title'.'stockName'.'stockCode'.'orgCode'.'orgName'.'orgSName'.'publishDate'.'predictNextTwoYearEps'.'predictNextTwoYearPe'.'predictNextYearEps'.'predictNextYearPe'.'predictThisYearEps'.'predictThisYearPe'.'indvInduCode'.'indvInduName'.'lastEmRatingName'.'lastEmRatingValue'.'emRatingValue'.'emRatingName'.'ratingChange'.'researcher'.'encodeUrl'.'count']
        insert_tuple = []
        for obj in data_list:
            insert_tuple.append((obj['title'], obj['stockName'], obj['stockCode'], obj['orgCode'], obj['orgName'], obj['orgSName'], obj['publishDate'], obj['predictNextTwoYearEps'], obj['predictNextTwoYearPe'], obj['predictNextYearEps'], obj['predictNextYearPe'], obj['predictThisYearEps'], obj['predictThisYearPe'], obj['indvInduCode'], obj['indvInduName'], obj['lastEmRatingName'], obj['lastEmRatingValue'], obj['emRatingValue'],obj['emRatingName'], obj['ratingChange'], obj['researcher'], obj['encodeUrl'], obj['count']))
        values_sql = ['%s' for v in attrs]
        attrs_sql = '('+', '.join(attrs)+') '
        values_sql = ' values('+', '.join(values_sql)+') '
        sql = 'insert into %s' % 'report'
        sql = sql + attrs_sql + values_sql
        try:
            print(sql)
            for i in range(0.len(insert_tuple), 20000):
                self.cur.executemany(sql, tuple(insert_tuple[i:i+20000]))
                self.conn.commit()
        except pymysql.Error as e:
            self.conn.rollback()
            error = 'insertMany executemany failed! ERROR (%s): %s' % (e.args[0], e.args[1])
            print(error)
Copy the code

After running the program, my database data looks like this:

Store the data

Using the data

Due to space limitations, this article first introduces the data acquisition part, as long as the data in hand, the rest is covered. But after all, it is stock investment, and any auxiliary decision data must be combined with the market data of individual stocks, so stock data is necessary, and the way of obtaining this data is introduced in our previous article. Let me outline how to use this data.

  • You can calculate the increase of individual stocks since the recommendation date based on a period of time (such as one month, six months, etc.) after the recommendation. If an analyst recommends several stocks with good earnings, the analyst can be added to the watch list, and the following recommended stocks can be focused on. I can also make a list of the most cattle analysts!
  • Stock ratings and stock rating changes are two conditions to take a look at. Personal experience, if it is the first recommendation to buy, can generally focus on look. In addition, if the rating change is in a good direction can also focus on the change, such as the last time was a “buy”, now become a “strong buy”, then it is worth focusing on.
  • Earnings per share and price-to-earnings ratios can also help. Can be based on the forecast of next year and the year after the earnings per share and p/E to calculate the approximate rise of the stock is expected. If the expected increase is relatively large, it can be focused on.

These directions can be analyzed according to the data. Of course, these directions only help us to screen stocks more quickly. We also need to carefully look at research reports and analyze the fundamentals of individual stocks before making a final decision.

conclusion

This paper introduces how to obtain individual stock research and report data of securities company analysts from the Website of Oriental Fortune. Based on the obtained data, it suggests several directions that can be analyzed and applied. Limited by space, there is no specific analysis of the specific use of detailed description, you can study. Of course, if you are interested in this aspect, please click a “watching”, if the number of “watching” is more than 30, I am willing to continue to write an article to introduce the subsequent analysis and application, for your reference.

PS: Reply “Python” within the public number, you can enter the Python novice learning exchange group, together with 100 days plan!

Old rules, brothers still remember, the lower right corner of the “see” point, if you feel the content of the article is good, remember to share circle of friends to let more people know!

【 Code access method ****】

Identify the qr code at the end of the text, reply: 1104