抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

This Python Project mini-course provided by IBM is intended to demonstrate basic Python skills by performing specific tasks such as extracting data, web scraping, visualizing data, and creating a dashboard. The following are the notes I took during this course.

Web Scraping

HTML Tags

  • HTML Composition
  • HTML Paragraph Tags: <p></p>
  • HTML Anchor Tag & Hyperlink Tag: <a href=""></a>
  • Attributes: href=""
  • Inspect HTML: F12
  • Document Tree: <head></head>, <body></body>
  • HTML Tables: <table><tr><td>1</td><td>2</td></tr></table>

BeautifulSoup

1
2
3
from bs4 import BeautifulSoup
html=""
soup=BeautifulSoup(html, 'html5lib')

BeautifulSoup: Object

1
2
3
4
5
6
7
tag_object=soup.title
tag_object=soup.h3
tag_child=tag_object.b #HTML Tree
parent_tag=tag_child.parent #Parent attribute
sibling_1=tag_object.next_sibling #Next-sibling attribute
tag_child.attrs #Show attributes
tag_child.string #Navigable string

BeautifulSoup: find_all

1
2
3
4
5
6
7
8
table=BeautifulSoup(html, 'html5lib')
table_row=table.find_all(name='tr') #Python iterable
first_row=table_row[0]
from i,row in enumerate(table_rows): #Elements
print("row",i)
cells+row.find_all("td")
for j,cell in enumerate(cells):
print("column",j,"cell",cell)

Requests & BeautifulSoup in real web page

1
2
3
4
5
6
7
8
9
10
import requests
from bs4 import BeautifulSoup
page=requests.get("http://...").text
soup=BeautifulSoup(page, "html.parser") #Create a BeautifulSoup object
artists=soup.find_all('a') #Pull all instances of <a> tag
for artist in artists: #Clears data of all tags
names=artist.contents[0]
fullLink=artist.get('href')
print(names)
print(fullLink)

Project: Analyzing Stock Performance and Building a Dashboard

A stock (also known as equity) is a security that represents the ownership of a fraction of a corporation.

The stock ticker is a report of the price of a certain stock, updated continuously throughout the trading session by the various stock market exchanges.

Extracting Stock Data Using a Python Library: yfinance

Extracting Stock Data Using Web Scraping

Project Notebook

Assignments

Visit my Github Repository

评论



Copyright © 2020 - 2022 Zhihao Zhuang. All rights reserved

本站访客数: 人,
总访问量: