2

hope anyone can help me. I am fairly new to python, but I want to scrape data from a site, which unfortunately needs an account. Although i am not able to extract the date (i.e. 2017-06-01).

<li class="latest-value-item">
  <div class="latest-value-label">Date</div>
  <div class="latest-value">2017-06-01</div>
</li>
<li class="latest-value-item">
  <div class="latest-value-label">Index</div>
  <div class="latest-value">1430</div>
</li>

THis is my code:

import urllib3 
import urllib.request
from bs4 import BeautifulSoup
import pandas as pd
import requests
import csv
from datetime import datetime

url = 'https://www.quandl.com/data/LLOYDS/BCI-Baltic-Capesize-Index'
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')

Baltic_Indices = []
New_Value = []

#new = soup.find_all('div', attrs={'class':'latest-value'}).get_text()
date = soup.find_all(class_="latest value")
text1 = date.text

print(text1)
doru
  • 9,022
  • 2
  • 33
  • 43
Savkeeeee
  • 33
  • 1
  • 4
  • Possible duplicate of [Extracting text from HTML file using Python](https://stackoverflow.com/questions/328356/extracting-text-from-html-file-using-python) – Umair Jun 02 '17 at 08:07

1 Answers1

3

date = soup.find_all(class_="latest value")

You are using the wrong CSS class name ('latest value' != 'latest-value')

 print(soup.find_all(attrs={'class': 'latest-value'}))
 # [<div class="latest-value">2017-06-01</div>, <div class="latest-value">1430</div>]

 for element in soup.find_all(attrs={'class': 'latest-value'}):
     print(element.text)
 # 2017-06-01
 # 1430

I prefer to use attrs kwarg but your method works as well (given the correct CSS class name)

 for element in soup.find_all(class_='latest-value'):
     print(element.text)
 # 2017-06-01
 # 1430
DeepSpace
  • 78,697
  • 11
  • 109
  • 154