2016-12-29 97 views
1

我有this site。我想提取出现在文章标题下方的符号(EXAS,ESNT,ENZ,CENT,AEE)。我是一名初学者,所以我尝试了一种相当反pythonic的方法:在网站中提取两个字符串的子串之间

import requests 
link="https://www.zacks.com/commentary/99386/new-strong-buy-stocks-for-december-29th" 
fetch_data = requests.get(link) 
content = str((fetch_data.content)) 
# I know that in the source code the symbols appear between "tickers" and "publish_date" therefore: 
tickers= "tickers :" 
pd = "publish_date :" 
Z= ("%s(.*)%s" % (tickers,pd)) 
result = re.search(Z, content) 
print (result) 
# Just printing out the substring between tickers and pd 
Output: <_sre.SRE_Match object; span=(95142, 95213), match="tickers : [\\'EXAS\\',\\'ESNT\\',\\'ENZ\\',\\'CEN> 

如何才能打印出符号?另外,最后一个符号'CEN'应该打印为'CENT'并且'AEE'符号也不存在。这将是理想的

Symbols: EXAS, ESNT, ENZ, CENT, AEE 

或者至少是:

"tickers : [\\'EXAS\\',\\'ESNT\\',\\'ENZ\\',\\'CENT\\',\\'AEE\\] 

回答

1

可以访问第一组和清理:

>>> tickers = result.groups()[0] 
>>> re.findall(r'\[.*?\]', tickers)[0].split("\\'")[1::2] 
['EXAS', 'ESNT', 'ENZ', 'CENT', 'AEE'] 
相关问题