An extremely concise Python web page capturing program

Source: Internet
Author: User

 

The Code is as follows:

 

import timeimport urllibimport sysimport stringfrom HTMLParser import HTMLParserticker_list = [socl, ibb, pnqi, eirl, ita, iai, xsd, vbk, dfe, qqq, ewi, pbd]ticker = class MyHTMLParser(HTMLParser):    previous_data =     text_before_amp =         def handle_data(self, data):        starttag_text = self.get_starttag_text()                if -1!=string.find(data, ((%s) % ticker).upper()) and -1!=string.find(starttag_text, 
): Sys. stdout. write (time. strftime (% d/% m/% Y) +) if! = Self. text_before_amp: sys. stdout. write (self. text_before_amp + &) sys. stdout. write (data) if-1! = String. find (str (starttag_text), yfs_g53 _ % s % ticker. lower () and-1 = string. find (data,-): sys. stdout. write (+ data) if-1! = String. find (str (starttag_text), yfs_h53 _ % s % ticker. lower (): print, data self. previous_data = data def handle_entityref (self, name): if amp = name and-1! = String. find (self. get_starttag_text (),): self. text_before_amp = self. previous_data for t in ticker_list: ticker = t parser = MyHTMLParser () f = urllib. urlopen (http://finance.yahoo.com/q? S = % s % ticker) html_string = f. read () parser. feed (html_string)

 

Sample output:

 

03/05/2014Global X Social Media Index ETF (SOCL)17.4117.8303/05/2014iShares Nasdaq Biotechnology (IBB)227.70233.0703/05/2014PowerShares NASDAQ Internet (PNQI)62.1562.9803/05/2014iShares MSCI Ireland Capped (EIRL)38.2838.7903/05/2014iShares US Aerospace & Defense (ITA)108.32109.7003/05/2014iShares US Broker-Dealers (IAI)37.4637.8903/05/2014SPDR S&P Semiconductor ETF (XSD)66.8967.5903/05/2014Vanguard Small Cap Growth ETF (VBK)119.46120.7003/05/2014WisdomTree Europe SmallCap Dividend (DFE)62.0262.6103/05/2014PowerShares QQQ (QQQ)87.2888.1103/05/2014iShares MSCI Italy Capped (EWI)17.8918.0303/05/2014PowerShares Global Clean Energy (PBD)13.0213.08

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.