Python Web刮股票图表,当找不到股票代码时代码卡住

问题描述 投票:-1回答:3

我有一个通过本网站运行的股票代码列表,然后希望获得股票图表的链接

但是,当符号出错时,网站会重定向到另一个页面,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

最好的结果是它获得了股票图表的所有链接,可以告诉我哪个股票代码遇到错误并继续运行剩余的符号

谢谢

python loops redirect web-scraping
3个回答
0
投票

当符号不存在时,不会抛出异常。这意味着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"

0
投票

有一个逻辑错误。这是一个改变,我认为会让你失意。

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

0
投票

稍微简洁并重用现有连接:

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)
© www.soinside.com 2019 - 2024. All rights reserved.