from lxml.html import parse
import urllib.request
parsed=parse(urllib.request.urlopen('http://finance.sina.com.cn/stock/'))
doc=parsed.getroot()
print (doc)
<Element html at 0x54db9f8>
links=doc.findall('.//a')
links[:3]
[<Element a at 0x55fe5e8>, <Element a at 0x55fe868>, <Element a at 0x55fe6d8>]
urls=[lnk.get('href') for lnk in doc.findall('.//a')]
urls[-10:]
['http://corp.sina.com.cn/chn/',
'http://corp.sina.com.cn/eng/',
'http://emarketing.sina.com.cn/',
'http://www.sina.com.cn/contactus.html',
'http://corp.sina.com.cn/chn/sina_job.html',
'http://www.sina.com.cn/intro/lawfirm.shtml',
'http://english.sina.com',
'https://login.sina.com.cn/signup/signup.php',
'http://help.sina.com.cn/',
'http://corp.sina.com.cn/chn/copyright.html']
tables=doc.findall('.//table')
call=tables[1]
rows=call.findall('.//tr')
def _unpack(row,kind='td'):
elts=row.findall('.//%s' % kind)
return [val.text_content() for val in elts]
_unpack(rows[0],kind='td')
['行业涨幅', '行业跌幅', '行业流入', '行业流出']