BeautifulSoup4 Find Method
up vote
0
down vote
favorite
I tried scraping some a number from yahoo finance using python3, but all I get is a "None".
from bs4 import BeautifulSoup
import requests
source = requests.get('https://finance.yahoo.com/quote/SWCH?
p=SWCH&.tsrc=fin-srch').text
soup = BeautifulSoup(source, 'lxml')
price = soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')
print(price)
Thanks,
R.Vij
python python-3.x beautifulsoup
add a comment |
up vote
0
down vote
favorite
I tried scraping some a number from yahoo finance using python3, but all I get is a "None".
from bs4 import BeautifulSoup
import requests
source = requests.get('https://finance.yahoo.com/quote/SWCH?
p=SWCH&.tsrc=fin-srch').text
soup = BeautifulSoup(source, 'lxml')
price = soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')
print(price)
Thanks,
R.Vij
python python-3.x beautifulsoup
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I tried scraping some a number from yahoo finance using python3, but all I get is a "None".
from bs4 import BeautifulSoup
import requests
source = requests.get('https://finance.yahoo.com/quote/SWCH?
p=SWCH&.tsrc=fin-srch').text
soup = BeautifulSoup(source, 'lxml')
price = soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')
print(price)
Thanks,
R.Vij
python python-3.x beautifulsoup
I tried scraping some a number from yahoo finance using python3, but all I get is a "None".
from bs4 import BeautifulSoup
import requests
source = requests.get('https://finance.yahoo.com/quote/SWCH?
p=SWCH&.tsrc=fin-srch').text
soup = BeautifulSoup(source, 'lxml')
price = soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)')
print(price)
Thanks,
R.Vij
python python-3.x beautifulsoup
python python-3.x beautifulsoup
edited Nov 11 at 2:54
asked Nov 11 at 2:48
R.Vij
176
176
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.
Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.
I revised your find statement to the following one:
soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
The following code returns the current price of SWCH
from bs4 import BeautifulSoup
import requests
source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
soup = BeautifulSoup(source, 'lxml')
price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
print(price.text) # 9.29 for now
Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
– R.Vij
Nov 11 at 3:53
Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the firstx andstuff) and then checks if the actual class names are a superset of the required class names.
– Eternal_flame-AD
Nov 11 at 3:58
add a comment |
up vote
0
down vote
'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:
soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())
#<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...
I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
– R.Vij
Nov 11 at 3:48
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.
Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.
I revised your find statement to the following one:
soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
The following code returns the current price of SWCH
from bs4 import BeautifulSoup
import requests
source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
soup = BeautifulSoup(source, 'lxml')
price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
print(price.text) # 9.29 for now
Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
– R.Vij
Nov 11 at 3:53
Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the firstx andstuff) and then checks if the actual class names are a superset of the required class names.
– Eternal_flame-AD
Nov 11 at 3:58
add a comment |
up vote
1
down vote
accepted
Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.
Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.
I revised your find statement to the following one:
soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
The following code returns the current price of SWCH
from bs4 import BeautifulSoup
import requests
source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
soup = BeautifulSoup(source, 'lxml')
price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
print(price.text) # 9.29 for now
Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
– R.Vij
Nov 11 at 3:53
Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the firstx andstuff) and then checks if the actual class names are a superset of the required class names.
– Eternal_flame-AD
Nov 11 at 3:58
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.
Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.
I revised your find statement to the following one:
soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
The following code returns the current price of SWCH
from bs4 import BeautifulSoup
import requests
source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
soup = BeautifulSoup(source, 'lxml')
price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
print(price.text) # 9.29 for now
Note that if you pass a list to the class_ kwarg bs4 will select elements that have ANY of the specified classNames in the document, not ALL of them.
Also you need to note that some of the class values are set dynamically using browser javascript so that they won't appear on the actual document.
I revised your find statement to the following one:
soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
The following code returns the current price of SWCH
from bs4 import BeautifulSoup
import requests
source = requests.get('https://finance.yahoo.com/quote/SWCH?p=SWCH&.tsrc=fin-srch').text
soup = BeautifulSoup(source, 'lxml')
price = soup.find('span', class_=lambda x:x and set(x.split()).issuperset(set("Trsdu(0.3s) Fw(b) Fz(36px) Fw(b) D(b) Mb(-4px)".split())))
print(price.text) # 9.29 for now
answered Nov 11 at 3:24
Eternal_flame-AD
3126
3126
Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
– R.Vij
Nov 11 at 3:53
Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the firstx andstuff) and then checks if the actual class names are a superset of the required class names.
– Eternal_flame-AD
Nov 11 at 3:58
add a comment |
Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
– R.Vij
Nov 11 at 3:53
Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the firstx andstuff) and then checks if the actual class names are a superset of the required class names.
– Eternal_flame-AD
Nov 11 at 3:58
Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
– R.Vij
Nov 11 at 3:53
Thanks, it worked. Do you know how the lambda stuff works and what it does? I like to know what code does.
– R.Vij
Nov 11 at 3:53
Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the first
x and stuff) and then checks if the actual class names are a superset of the required class names.– Eternal_flame-AD
Nov 11 at 3:58
Beautifulsoup4 would call the lambda function for every element with the value of the class attribute of that element(None if the class attribute does not exist) and select the element if the lambda function returns True. The lambda function checks if the class attribute exists(the first
x and stuff) and then checks if the actual class names are a superset of the required class names.– Eternal_flame-AD
Nov 11 at 3:58
add a comment |
up vote
0
down vote
'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:
soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())
#<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...
I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
– R.Vij
Nov 11 at 3:48
add a comment |
up vote
0
down vote
'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:
soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())
#<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...
I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
– R.Vij
Nov 11 at 3:48
add a comment |
up vote
0
down vote
up vote
0
down vote
'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:
soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())
#<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...
'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)' is not a class but five classes. If you want to find any of them, you should pass them as a list:
soup.find('span', class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'.split())
#<span class="D(ib) W($privatePromoMsgWidth) Fz(12px) Fw(500) Va(m) Wob(n)"...
answered Nov 11 at 2:54
DYZ
24k61948
24k61948
I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
– R.Vij
Nov 11 at 3:48
add a comment |
I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
– R.Vij
Nov 11 at 3:48
I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
– R.Vij
Nov 11 at 3:48
I tried, but what I got inside the tags was "Now you can search stock related news and private companies such as Airbnb." The result should be "9.29".
– R.Vij
Nov 11 at 3:48
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245424%2fbeautifulsoup4-find-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown