鱼C论坛

 找回密码
 立即注册
查看: 569|回复: 1

出现错误,不知道怎么解决了

[复制链接]
发表于 2018-12-30 17:07:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import requests
from bs4 import BeautifulSoup
import traceback
import re
def getHTMLText(url):
     try:
        r=requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
     except:
        return""
   
def getStockList(lst,stockURL):
    html=getHTMLText(stockURL)
    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):
    for stock in lst:
        url=stockURL+stock+".html"
        html=getHTMLText(url)
        try:
            if html=="":
                continue
            infoDict={}
            soup=BeautifulSoup(html,"html.parser")
            stockInfo=soup.find("div",attrs={"class":"stock-bets"})
            
            name=stockInfo.find_all(attrs={"class":"bets-name"})[0]
            infoDict.update({"股票名称":name.text.split()[0]})

            keyList=stockInfo.find_all("dt")
            valueList=stockInfo.find_all("dd")
            for i in range(len(keyList)):
                key=keyList[i].text
                val=valueList[i].text
                infoDict[key]=val

            with open(fpath,"a",encoding="utf-8") as f:
                f.write(str(infoDict)+"\n")
        except:
            traceback.print_exc()
            continue

            
def main():
    stock_list_url="http://quote.eastmoney.com/stocklist.html"
    stock_info_url="https://gupiao.baidu.com/stock/"
    output_file="E://BaiduStockInfo.txt"
    slist=[]
    getStockList(slist,stock_list_url)
    getStockInfo(slist,stock_info_url,output_file)
main()

Traceback (most recent call last):
File "E:\xiaojiayu\股票数据定向爬虫.py", line 37, in getStockInfo
name=stockInfo.find_all(attrs={"class":"bets-name"})[0]
AttributeError: 'NoneType' object has no attribute 'find_all'出现这种错误,怎么解决]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-30 17:10:28 | 显示全部楼层
那就是没有这个标签,没找到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-12 18:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表