# # -*- coding: utf-8 -*-
# import scrapy
# import re

# class StocksSpider(scrapy.Spider):
#     name = 'stocks'
#     start_urls = ['http://quote.eastmoney.com/stocklist.html']

#     def parse(self, response):
#         for href in response.css('a::attr(href)').extract():
#             try:
#                 stock = re.findall(r"[s][hz[\d{6}",href)[0]
#                 url = 'https://gupiao.baidu.com/stock/' + stock + '.html'
#                 yield scrapy.Request(url,callback = self.parse_stock)
#             except:
#                 continue

#     def parse_stock(self,response):
#         infoDict = {}
#         stockInfo = response.css('.stock-bets')
#         name = stockInfo.css('.beta-name').extract()[0]
#         keyList = stockInfo.css('dt').extract()
#         valueList = stockInfo.css('dt').extract()
#         for i in range(len(keyList)):
#             key = re.findall(r'>.*</dt>',keyList[i])[0][1:-5]
#             try:
#                 val = re.findall(r'\d+\.?.*</dd>',valueList[i])[0][0:-5]
#             except :
#                 val = '--'
#             infoDict[key] = val

#             infoDict.update(
#                 {'股票名称':re.findall('\s.*\(',name)[0].spilt()[0] + \
#                     re.findall('\>.*\<',name)[0][1:-1]})
#             yield infoDict
import scrapy
from bs4 import BeautifulSoup
import re

class StockSpider(scrapy.Spider):
    name = 'stocks'
    # allowed_domains = ['quote.eastmoney.com']
    start_urls = ['http://quote.eastmoney.com/stock_list.html']
    def parse(self, response):
        for href in response.css('a::attr(href)').extract():
            try:
                stock = re.search(r"[s][hz]\d{6}", href).group(0)
                stock = stock.upper()
                url = 'https://xueqiu.com/S/' + stock
                yield scrapy.Request(url, callback = self.parse_stock)
            except:
                continue
    def parse_stock(self, response):
        infoDict = {}
        if response == "":
            exit()
        try:
            name = re.search(r'<div class="stock-name">(.*?)</div>', response.text).group(1)
            infoDict.update({'股票名称': name.__str__()})
            tableHtml = re.search(r'"tableHtml":"(.*?)",', response.text).group(1)
            soup = BeautifulSoup(tableHtml, "html.parser")
            table = soup.table
            for i in table.find_all("td"):
                line = i.text
                l = line.split("：")#这里的冒号为中文的冒号(：)!!!而不是英文的(:)
                infoDict.update({l[0].__str__(): l[1].__str__()})
            yield infoDict
        except:
            print("error")