BeautifulSoup webpage scraping

web scraping python beautifulsoup code
how to scrape data from website using python 3
beautifulsoup looping through pages
how to scrape websites
how to use beautifulsoup
web scraping using python code
beautifulsoup tutorial | python 3
beautifulsoup crawler

I am trying to scrap a webpage.

from bs4 import BeautifulSoup
import requests

page = requests.get('https://www.mql5.com/en/economic-calendar/united-states')
soup = BeautifulSoup(page.content, 'html.parser')
calender = soup.find(id="economicCalendarTable")
items = calender.find_all(class_="ec-table__title")
print(items)

However, it prints an empty list, although in the webpage there are many entries with tag "class_="ec-table__title". What I found is that the tags inside "id="economicCalendarTable" tag are just in one line (very long). So, "calender.find_all" skips everything.

I am trying to get all tages inside 'id="economicCalendarTable"'.

Is there way to do this?

You can use selenium:

from selenium import webdriver
import re
from bs4 import BeautifulSoup as soup
d = webdriver.Chrome()
d.get('https://www.mql5.com/en/economic-calendar/united-states')
s = soup(d.page_source, 'lxml')
time = s.find('span', {'id':'economicCalendarTableColumnTime'}).text
title = s.find('div', {'class':'ec-table__title'}).text
classes = ['ec-table__col_time', 'ec-table__curency-name', 'ec-table__col_event', 'ec-table__col_forecast', 'prevValue']
full_data = [[i.find('div', {'class':c if c != 'prevValue' else re.compile('prevValue\d+')}) for c in classes] for i in s.find_all('div', {'class':'ec-table__item'})]
new_results = [dict(zip(['time', 'name', 'event', 'forcast', 'prevous_value'], [getattr(i, 'text', '') for i in b])) for b in full_data]

Output:

[{'event': u'Chicago Fed National Activity Index', 'forcast': u'0.14', 'name': u'USD', 'prevous_value': '', 'time': u' 08:30'}, {'event': u'Markit Manufacturing PMI', 'forcast': u'56.4', 'name': u'USD', 'prevous_value': '', 'time': u' 09:45'}, {'event': u'Markit Services PMI', 'forcast': u'55', 'name': u'USD', 'prevous_value': '', 'time': u' 09:45'}, {'event': u'Markit Composite PMI', 'forcast': u'55', 'name': u'USD', 'prevous_value': '', 'time': u' 09:45'}, {'event': u'New Home Sales m/m', 'forcast': u'-1.2%', 'name': u'USD', 'prevous_value': '', 'time': u' 10:00'}, {'event': u'New Home Sales', 'forcast': u'0.639 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:00'}, {'event': u'EIA Crude Oil Stocks Change', 'forcast': u'-1.791 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:30'}, {'event': u'EIA Cushing Crude Oil Stocks Change', 'forcast': u'0.259 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:30'}, {'event': u'EIA Crude Oil Imports Change', 'forcast': u'-0.32 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:30'}, {'event': u'EIA Distillate Fuel Production Change', 'forcast': u'-0.011 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:30'}, {'event': u'EIA Distillates Stocks Change', 'forcast': u'-0.182 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:30'}, {'event': u'EIA Gasoline Production Change', 'forcast': u'0.289 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:30'}, {'event': u'EIA Heating Oil Stocks Change', 'forcast': u'-0.026 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:30'}, {'event': u'EIA Gasoline Stocks Change', 'forcast': u'-3.206 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:30'}, {'event': u'FOMC Minutes', 'forcast': u'', 'name': u'USD', 'prevous_value': '', 'time': u' 14:00'}, {'event': u'Continuing Jobless Claims', 'forcast': u'1.769 M', 'name': u'USD', 'prevous_value': '', 'time': u' 08:30'}, {'event': u'Initial Jobless Claims', 'forcast': u'216 K', 'name': u'USD', 'prevous_value': '', 'time': u' 08:30'}, {'event': u'Initial Jobless Claims 4-Week Average', 'forcast': u'213.814 K', 'name': u'USD', 'prevous_value': '', 'time': u' 08:30'}, {'event': u'HPI m/m', 'forcast': u'0.5%', 'name': u'USD', 'prevous_value': '', 'time': u' 09:00'}, {'event': u'Existing Home Sales', 'forcast': u'5.45 M', 'name': u'USD', 'prevous_value': '', 'time': u' 10:00'}, {'event': u'Existing Home Sales m/m', 'forcast': u'0.3%', 'name': u'USD', 'prevous_value': '', 'time': u' 10:00'}, {'event': u'EIA Natural Gas Storage Change', 'forcast': u'92 B', 'name': u'USD', 'prevous_value': '', 'time': u' 10:30'}, {'event': u'Durable Goods Orders m/m', 'forcast': u'-0.3%', 'name': u'USD', 'prevous_value': '', 'time': u' 08:30'}, {'event': u'Core Durable Goods Orders m/m', 'forcast': u'0.0%', 'name': u'USD', 'prevous_value': '', 'time': u' 08:30'}, {'event': u'Durable Goods Orders excl. Defense m/m', 'forcast': u'-6.2%', 'name': u'USD', 'prevous_value': '', 'time': u' 08:30'}, {'event': u'Nondefense Capital Goods Orders excl. Aircraft m/m', 'forcast': u'0.3%', 'name': u'USD', 'prevous_value': '', 'time': u' 08:30'}, {'event': u'Fed Chair Powell Speech', 'forcast': u'', 'name': u'USD', 'prevous_value': '', 'time': u' 09:20'}, {'event': u'Michigan Consumer Sentiment', 'forcast': u'98.5', 'name': u'USD', 'prevous_value': '', 'time': u' 10:00'}, {'event': u'Michigan Consumer Expectations', 'forcast': u'88.9', 'name': u'USD', 'prevous_value': '', 'time': u' 10:00'}, {'event': u'Michigan Current Conditions', 'forcast': u'112.9', 'name': u'USD', 'prevous_value': '', 'time': u' 10:00'}, {'event': u'Michigan Inflation Expectations', 'forcast': u'2.7%', 'name': u'USD', 'prevous_value': '', 'time': u' 10:00'}, {'event': u'Michigan 5-Year Inflation Expectations', 'forcast': u'2.5%', 'name': u'USD', 'prevous_value': '', 'time': u' 10:00'}, {'event': u'Baker Hughes US Oil Rig Count', 'forcast': u'', 'name': u'USD', 'prevous_value': '', 'time': u' 13:00'}, {'event': u'CFTC Copper Non-Commercial Net Positions', 'forcast': u'', 'name': u'USD', 'prevous_value': '', 'time': u' 15:30'}, {'event': u'CFTC Crude Oil Non-Commercial Net Positions', 'forcast': u'', 'name': u'USD', 'prevous_value': '', 'time': u' 15:30'}, {'event': u'CFTC S&P 500 Non-Commercial Net Positions', 'forcast': u'', 'name': u'USD', 'prevous_value': '', 'time': u' 15:30'}, {'event': u'CFTC Gold Non-Commercial Net Positions', 'forcast': u'', 'name': u'USD', 'prevous_value': '', 'time': u' 15:30'}, {'event': u'CFTC Silver Non-Commercial Net Positions', 'forcast': u'', 'name': u'USD', 'prevous_value': '', 'time': u' 15:30'}]

Beautiful Soup: Build a Web Scraper With Python – Real Python, One way to get all the data we need is to compile a list of movie names, and use it to access the web page of each movie on both IMDB and Metacritic websites. Point and click web scraping tool in cloud. Signup now to get 100 pages free. Scalable web scraping software to scrape data from websites anonymously.

Here's a simple example I've put together using Selenium and BeautifulSoup:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Selenium part
browser = webdriver.Chrome()
browser.get('https://www.mql5.com/en/economic-calendar/united-states')

# BeautifulSoup part
source = browser.page_source
soup = BeautifulSoup(source, 'html.parser')
calender = soup.find(id="economicCalendarTable")
items = calender.find_all(class_="ec-table__title")
print(items)

This code will allow you to download the page entirely and then pass the complete html source to BS

Be sure to install Selenium and the ChromeDriver correctly before running this script.

Tutorial: Web Scraping and BeautifulSoup – Dataquest, We'll make data extraction easier by building a web scraper to retrieve stock indices automatically from the Internet. Getting Started. We are going  Try Wix's Web Page Design Tools, Designer-Made Templates & Top Customization Features. Wix Offers the Freedom You Need to Create Your Own Unique Online Look.

There is no item with class ec-table__title in the base html of that page.

However, it does appear when using a dom inspector in the browser. I am afraid this is a sure sign that it has been inserted into the DOM by javascript ad indeed there is some javascript invoked by that webpage.

May I suggest that you investigate using the selenium module in conjunction with BeautifulSoup?

How to scrape websites with Python and BeautifulSoup, Web scraping allows you to download the HTML of a website and extract the data that you need. Using Python with Beautiful Soup is an  Easy & Cheap Site Builders. Get your Free Domain and Build A Website Now

Quick Web Scraping with Python and Beautiful Soup, Web Scraping is the process of downloading data from websites and HTML data and BeautifulSoup which is used as a parser for the HTML. #1 Freelancing Site. Get Quotes in Minutes & Pick Your Web Scraping Expert Today

Introduction to Web Scraping with BeautifulSoup, For example, a python script could scrape a website when ticket sales But Beautiful Soup allows you to parse the HTML in a a beautiful way,  Web Scraping with BeautifulSoup Web Scraping "Web scraping (web harvesting or web data extraction) is a computer software technique of extracting information from websites."

Web Scraping 101 with Python & Beautiful Soup, HTML parsing is easy in Python, especially with help of the BeautifulSoup library. In this post we will scrape a website (our own) to extract all  To parse our HTML document and extract the 50 div containers, we’ll use a Python module called BeautifulSoup, the most common web scraping module for Python. In the following code cell we will: Import the BeautifulSoup class creator from the package bs4.

Comments
  • The page is dynamically generated, so when you use requests to download it, all those elements with ec-table__title aren't in the page source. If you want to, you can use selenium, a code-controlled web browser, to wait for the page to load, and then scrape the page.
  • I do not see ec-table__title anywhere. Where are those class located?
  • Just under id="economicCalendarTable" and below.
  • Ah, I see @manojg! I've prepared a solution, you can find it below.