Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Extract Specific Data from Webscraping

I’m trying to extract specific data from a webpage using this code:

from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.dailyfx.com/sentiment-report').text

soup = BeautifulSoup(source, 'lxml')
#print(soup.prettify())
price = soup.find_all('span', {'class' : 'gsstx'})


print(price)

but it just prints out everything in the gsstx span class like this:

</span>, <span class="gsstx" style="font-weight:bold;">Retail trader data shows 76.23% of traders are net-long with the ratio of traders long to short at 3.21 to 1. </span>, <span class="gsstx" style="font-weight:bold;">We typically take a contrarian view to crowd sentiment, and the fact traders are net-long suggests USD/CHF prices may continue to fall. </span>, <span class="gsstx" style="font-weight:bold;">Retail trader data shows 31.96% of traders are net-long with the ratio of traders short to long at 2.13 to 1. </span>, <span class="gsstx" style="font-weight:bold;">We typically take a contrarian view to crowd sentiment, and the fact traders are net-short suggests USD/JPY prices may continue to rise. </span>, <span class="gsstx" style="font-weight:bold;">**Retail trader data shows 44.10% of traders are net-long with the ratio of traders short to long at 1.27 to 1.** </span>, <span class="gsstx" style="font-weight:bold;">We typically take a contrarian view to crowd sentiment, and the fact traders are net-short suggests Wall Street prices may continue to rise. </span>]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I just print out this part?

Retail trader data shows 44.10% of traders are net-long with the ratio of traders short to long at 1.27 to 1.

>Solution :

For extracting and saving in csv, use following code (SPECIFIC TO THIS USE CASE AS SKIP MACHANSIM WILL NOT BE SAME IN ALL CASES)

from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.dailyfx.com/sentiment-report').text

soup = BeautifulSoup(source, 'lxml')
#print(soup.prettify())
price = soup.find_all('span', {'class' : 'gsstx', 'style':"font-weight:bold;"})
lis = []
skip = 0
for i in price:
    if skip%2==0:
        lis.append(i.get_text())
    skip+=1

df = pd.DataFrame({"Data":lis})
df.to_csv("Data.csv",index=False)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading