Python Web抓取股票图表,未找到股票代码时代码卡住

时间:2019-03-25 06:29:25

标签: python loops redirect web-scraping

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

但是,当符号出现错误时,网站会重定向到另一个页面,而python会停止运行其余符号

我的符号列表是:WOW,AAR,TPM

错误发生在AAR

有人可以给这个Py新手一些指导吗?


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

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

谢谢

3 个答案:

答案 0 :(得分: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"

答案 1 :(得分: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

答案 2 :(得分: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)