我有一个通过本网站运行的股票代码列表,然后希望获得股票图表的链接
但是,当符号出错时,网站会重定向到另一个页面,python会停止运行其余符号
我的符号列表是:WOW,AAR,TPM
错误发生在AAR
有谁能请给这个Py noob一些指导?
from urllib import urlopen
from bs4 import BeautifulSoup
import re
newsymbolslist = ['WOW','AAR','TPM']
i=0
try:
while i < len(newsymbolslist):
try:
html = urlopen( 'http://bigcharts.marketwatch.com/quickchart/quickchart.asp?symb=AU%3A'+newsymbolslist[i])
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src': re.compile('market')})
for image in images:
print (image['src'] + '\n')
i += 1
except:
print "error"
i += 1
except:
pass
最好的结果是它获得了股票图表的所有链接,可以告诉我哪个股票代码遇到错误并继续运行剩余的符号
谢谢
当符号不存在时,不会抛出异常。这意味着i
不会增加,因为它在for循环内迭代所找到的图像(在AAR情况下只是一个空列表)。结果是,i
永远不会满足于打破while循环的条件,它会永远持续下去。将i+=1
移动到finally块可确保它始终递增。
from urllib import urlopen
from bs4 import BeautifulSoup
import re
newsymbolslist = ['WOW','AAR','TPM']
i=0
try:
while i < len(newsymbolslist):
try:
html = urlopen( 'http://bigcharts.marketwatch.com/quickchart/quickchart.asp?symb=AU%3A'+newsymbolslist[i])
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src': re.compile('market')})
for image in images:
print (image['src'] + '\n')
except Exception as e:
print "error"
finally:
i += 1
except:
pass
作为改进,您可以通过遍历您拥有的符号列表来完全删除while循环。然后你不必担心增加i
:
for symbol in newsymbolslist:
try:
html = urlopen( 'http://bigcharts.marketwatch.com/quickchart/quickchart.asp?symb=AU%3A'+symbol)
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src': re.compile('market')})
for image in images:
print (image['src'] + '\n')
except Exception as e:
print "error"
有一个逻辑错误。这是一个改变,我认为会让你失意。
from urllib import urlopen
from bs4 import BeautifulSoup
import re
newsymbolslist = ['WOW','AAR','TPM']
i=0
try:
while i < len(newsymbolslist):
try:
html = urlopen( 'http://bigcharts.marketwatch.com/quickchart/quickchart.asp?symb=AU%3A'+newsymbolslist[i])
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src': re.compile('market')})
for image in images:
print (image['src'] + '\n')
i += 1
except:
print "error"
i += 1
except:
pass
这可能有点简单:
from urllib import urlopen
from bs4 import BeautifulSoup
import re
newsymbolslist = ['WOW','AAR','TPM']
try:
for symbol in newsymbolslist:
try:
html = urlopen( 'http://bigcharts.marketwatch.com/quickchart/quickchart.asp?symb=AU%3A'+symbol)
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src': re.compile('market')})
for image in images:
print (image['src'] + '\n')
except:
print "error"
except:
pass
稍微简洁并重用现有连接:
import requests
from bs4 import BeautifulSoup
newSymbolsList = ['WOW','AAR','TPM']
with requests.Session() as s:
for symbol in newSymbolsList:
try:
html = s.get('http://bigcharts.marketwatch.com/quickchart/quickchart.asp?symb=AU%3A'+ symbol).content
bs = BeautifulSoup(html, 'lxml')
images = [img['src'] for img in bs.select('img[src*=market]')]
print(images)
except Exception as e:
print("error", e)