简体   繁体   English

错误:AttributeError:'NoneType' 对象没有属性 'text'

[英]Error: AttributeError: 'NoneType' object has no attribute 'text'

i have to simillar codes, one has multithreading and the other one doesnt.我必须使用类似的代码,一个有多线程,另一个没有。 the one that has multithreading is getting this error AttributeError: 'NoneType' object has no attribute 'text' while the other isnt, here is my code:多线程的一个得到这个错误AttributeError: 'NoneType' object has no attribute 'text'而另一个不是,这是我的代码:

multithreading:多线程:

import threading
import requests
from bs4 import BeautifulSoup

symbolsfile = open("Stocklist.txt")

symbolslist = symbolsfile.read()

thesymbolslist = symbolslist.split("\n")

print (thesymbolslist)


print_lock = threading.Lock()

def th(ur):
    theurl = "http://money.cnn.com/quote/quote.html?symb=" + ur
    thepage = requests.get(theurl)
    soup = BeautifulSoup(thepage.content,"html.parser")
    textfind = soup.find('span',{"stream":"last_36276"})
    texttext = textfind.text
    with print_lock:
        print(textfind)

threadlist = []

for u in thesymbolslist:
    t = threading.Thread(target = th, args=(u,))
    t.start()

    threadlist.append(t)

for b in threadlist:
    b.join()

and the one without multithreading:和一个没有多线程的:

import requests
from bs4 import BeautifulSoup


theurl = "http://money.cnn.com/quote/quote.html?symb=" + "AAPL"
thepage = requests.get(theurl)
soup = BeautifulSoup(thepage.content,"html.parser")
textfind = soup.find('span',{"stream":"last_36276"})
texttext = textfind.text
print(texttext)

The multihreading code works perfectly fine on my system (Win10, Python 3.4-64bit) when symbolslist is set to ['AAPL'] . symbolslist设置为['AAPL']时,多行代码在我的系统(Win10,Python 3.4-64位)上可以symbolslist But the script crashes with the reported error when symbolslist is set to ['AAPL', 'IBM'] . 但是,当symbolslist设置为['AAPL', 'IBM']时,脚本因报告的错误而崩溃。

When inspecting the returned HTML code, you can see that once stream="last_151846" and once stream="last_36276" is used. 检查返回的HTML代码时,可以看到一次使用stream="last_151846"和一次使用stream="last_36276" When you change the textfind line to 当您将textfind行更改为

textfind = soup.find('span',{"streamformat":"ToHundredth"})

or 要么

textfind = soup.find('span',{"streamfeed":"SunGard"})

the code will work for those examples, and hopefully for the ones provided in Stocklist.txt . 该代码将适用于这些示例,并希望适用于Stocklist.txt提供的Stocklist.txt

If your problem includes pytube in version 11.0.1.如果您的问题包括版本 11.0.1 中的 pytube。 I suggest you check this post:我建议你看看这个帖子:

pytube: AttributeError: 'NoneType' object has no attribute 'span' pytube: AttributeError: 'NoneType' 对象没有属性 'span'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:'NoneType'对象没有属性'text'-Python,BeautifulSoup错误 - AttributeError: 'NoneType' object has no attribute 'text' - Python , BeautifulSoup Error 错误消息说“AttributeError:'NoneType' object 没有属性'text'” - Error message saying "AttributeError: 'NoneType' object has no attribute 'text'" python错误AttributeError:'NoneType'对象没有属性'text' - python error AttributeError: 'NoneType' object has no attribute 'text' Python 错误消息 - AttributeError: 'NoneType' object 没有属性 'text' - Python Error Msg - AttributeError: 'NoneType' object has no attribute 'text' AttributeError:'NoneType'对象没有属性'text'-python - AttributeError: 'NoneType' object has no attribute 'text' - python Python:AttributeError:'NoneType'对象没有属性'text' - Python: AttributeError: 'NoneType' object has no attribute 'text' AttributeError: 'NoneType' 对象没有属性 'text' - AttributeError: 'NoneType' object has no attribute 'text' Webscraping AttributeError:“ NoneType”对象没有属性“ text” - Webscraping AttributeError: 'NoneType' object has no attribute 'text' BeautifulSoup“AttributeError: 'NoneType' 对象没有属性 'text'” - BeautifulSoup "AttributeError: 'NoneType' object has no attribute 'text'" BeautifulSoup AttributeError: 'NoneType' 对象没有属性 'text' - BeautifulSoup AttributeError: 'NoneType' object has no attribute 'text'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM