[英]AttributeError: 'NoneType' object has no attribute 'text' - Python , BeautifulSoup Error
[英]Error: AttributeError: 'NoneType' object has no attribute 'text'
我必須使用類似的代碼,一個有多線程,另一個沒有。 多線程的一個得到這個錯誤AttributeError: 'NoneType' object has no attribute 'text'而另一個不是,這是我的代碼:
多線程:
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()
和一個沒有多線程的:
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)
當symbolslist
設置為['AAPL']
時,多行代碼在我的系統(Win10,Python 3.4-64位)上可以symbolslist
。 但是,當symbolslist
設置為['AAPL', 'IBM']
時,腳本因報告的錯誤而崩潰。
檢查返回的HTML代碼時,可以看到一次使用stream="last_151846"
和一次使用stream="last_36276"
。 當您將textfind
行更改為
textfind = soup.find('span',{"streamformat":"ToHundredth"})
要么
textfind = soup.find('span',{"streamfeed":"SunGard"})
該代碼將適用於這些示例,並希望適用於Stocklist.txt
提供的Stocklist.txt
。
如果您的問題包括版本 11.0.1 中的 pytube。 我建議你看看這個帖子:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.