今天按照视频上的步骤爬起股票数据时,控制台出现了
name = stockInfo.find_all(attrs={'class':'bets-name'})[0]
AttributeError: 'NoneType' object has no attribute 'find_all'
的错误信息。
百度搜索了一下,发现大家都有这样的错误提示。在网上暂时没有找到解决这个错误的办法。
所以我就把自己的想法和大家分享一下,希望对大家有所帮助,同时也希望大家能提出意见和建议。
出现错误提示的代码:
import requests
from bs4 import BeautifulSoup
import re
import traceback
def getHTMLText(url,code='utf-8'):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = code
return r.text
except:
return ""
def getStockList(lst,stockURL):
html = getHTMLText(stockURL,'GB2312')
soup = BeautifulSoup(html,'html.parser')
a = soup.find_all('a')
for i in a:
try:
href = i.attrs['href']
lst.append(re.findall(r"[s][hz]\d{6}",href)[0])
except:
continue
def getStockInfo(lst,stockURL,fpath):
count = 0
for stock in lst:
url = stockURL + stock + ".html"
html = getHTMLText(url)
try:
if htm