Python Crawler Filters Stocks

Source: Internet
Author: User

Recently opened a stock account, so in Python wrote a script to filter out the 2015 1 quarter (open and close average) to now have a profit but less than 20% of the low-priced shares (price can be customized). See the code. Python is version 3.4.

<span style= "FONT-SIZE:14PX;" >import http.cookiejarimport urllib.requestimport jsonimport refrom bs4 import beautifulsoupdef GetOpener (head): # D EAL with the cookie CJ = Http.cookiejar.CookieJar () Pro = Urllib.request.HTTPCookieProcessor (CJ) opener = Urllib . Request.build_opener (Pro) header = [] for key, value in Head.items (): Elem = (key, value) Header.appe nd (elem) opener.addheaders=header return openerhead = {' Host ': ' vip.stock.finance.sina.com.cn ', ' Connection ': ' Keep-alive ', ' Accept ': ' text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ', ' accept-language ': ' zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3 ', ' user-agent ': ' mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) gecko/20100101 firefox/38.0 '}opener = Getopener (head) baseurl= ' http://vip.stock.finance.sina.com.cn/quotes _service/api/json_v2.php/market_center.gethqnodedata?num=80&sort=trade&asc=1&node=hs_a&symbol= &_s_r_a=page&page= ' stop=falsestopnum=15.01num=0for I in range (4,20): Print ("=================page:" +str (i) + "====================") Url=baseurl+str (i) op =opener.open (URL) data=op.read (). Decode (' GBK ') data=re.sub (' Ticktime: "(. *?)", ', ' ", data) Data=data.replace (' {') , ' {'). Replace (': ', ' ": '). Replace (', ', ', '). Replace (', ' {" ', ', {"') #print (data) data=json.loads (data) for J in rang                E (Len (data)): Trade=float (data[j][' trade ')) if Trade>0:if trade<float (stopnum): symbol=data[j][' symbol '] name=data[j][' name ' code=data[j][' code '] trade                 2=data[j][' Trade '] pricechange=data[j][' pricechange '] changepercent=data[j][' changepercent '] volume=data[j][' volume ' amount=data[j][' Amount '] c_url= ' Http://money.financ E.sina.com.cn/corp/go.php/vms_markethistory/stockid/' +str (code) + '. Phtml?year=2015&jidu=1 ' C_OP=OPENER.O              Pen (C_url)  C_data=c_op.read (). Decode ("GBK") Soup=beautifulsoup (c_data) tabdata=soup.find (' table ', id= ' Fundholdsharestable ') tabdata=str (tabdata) pattern=re.compile (' <td><div align= ' cent ER "> (. *?) </div></td> ', Re.                S) Items=re.findall (pattern, tabdata) sum=0 total=0 sum_num=0                        For item in Items:try:value=float (item) total=total+1 if Total%2!=0:sum=sum+value sum_                       Num=sum_num+1 except:continue If sum_num==0: Continue Avg=sum/sum_num price=float (trade2) print (name+ " Lastprice: "+STR (avg) +" Currentprice: "+str (Price) +" Changeprecent: "+str ((PRICE-AVG)/avg)) IF (price-avg>0.0) & ((PRICE-AVG)/avg<0.2): #print (' ******************************* ')  Print (' Stock name: ', name) print (' Stock code: ', Code) print (' Current Price: ', Trade2 ' print (' Up/Down: ', pricechange) print ('%: ', Changepe rcent) Print (' Turnover (Lot): ', volume) print (' Turnover (million): ', amount) nu m=num+1 print (' ************* ' +str (num) + ' ****************** ') else:c         Ontinue else:stop=true Break Else:continue if stop==true: Break print ("Find You" +str (num) + "only the stocks that meet your requirements") </span>

Python Crawler Filters Stocks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.