[PYTHON] Frequently used methods of Selenium and Beautiful Soup

I always get lost in coding Selenium and Beautiful Soup, so make a note for myself. I also post jQuery.

environment

pip

pip install requests
pip install beautifulsoup4

Selenium (Python)

#Initialization
from selenium import webdriver
driver = webdriver.Chrome()
url = 'https://qiita.com/users'
driver.get(url)

#Get one element(css selector)
element1 = driver.find_element_by_css_selector('.UsersPage__header')
#Get text
print(element1.text)

#Get multiple elements(css selector)
elements = driver.find_elements_by_css_selector('.UsersPage__user')
for elem in elements:
    #Get attributes
    href = elem.find_element_by_tag_name('a').get_attribute('href')
    print('{}<{}>'.format(elem.text, href))
    
driver.quit()

Beautiful Soup (Python)

#Initialization
from bs4 import BeautifulSoup
import requests
url = 'https://qiita.com/users'
resp = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'})
# resp.encoding = resp.apparent_encoding #Add if garbled characters
html = resp.text
soup = BeautifulSoup(html, 'html.parser')

#Get one element(css selector)
element1 = soup.select_one('.UsersPage__header')
#Get text
print(element1.get_text())

#Get multiple elements(css selector)
elements = soup.select('.UsersPage__user')
for elem in elements:
    #Get attributes
    href = elem.find('a').attrs['href']
    print('{}<{}>'.format(elem.get_text(), href))

jQuery (JavaScript)

//Initialization
location.href = "https://qiita.com/users";
var s=document.createElement("script");
s.setAttribute("src","https://code.jquery.com/jquery-2.2.4.min.js");
document.body.append(s);

//Get one element(css selector)
const $element1 = $(".UsersPage__header");
//Get text
console.log($element1.text());

//Get multiple elements(css selector)
const $elements = $(".UsersPage__user");
$elements.each(function(i,elem) {
  let $elem = $(elem);
  //Get attributes
  let href = $elem.find("a").attr("href");
  console.log(`${$elem.text()}<${href}>`);
});

Recommended Posts

Frequently used methods of Selenium and Beautiful Soup
List of frequently used built-in functions and methods
Selenium webdriver Summary of frequently used operation methods
Comparison table of frequently used processes of Python and Clojure
Automated testing method combining Beautiful Soup and Selenium (Python)
Frequently used subpackages of SciPy
[Python] A memorandum of beautiful soup4
List of frequently used Linux commands
[Anaconda3] Summary of frequently used commands
Paiza Skill Check List of Frequently Used D and C Ranks ~ Python ~
[Linux] Review of frequently used basic commands 2
Summary of frequently used commands of django (beginner)
Summary of methods often used in pandas
Beautiful Soup
Summary of frequently used commands in matplotlib
[Python3] Understand the basics of Beautiful Soup
Python + Selenium Frequently used operation method summary
Sort anime faces by scraping anime character pages with Beautiful Soup and Selenium
Screenshots of Megalodon in selenium and Chrome.
[Linux] Review of frequently used basic commands
[Machine learning] List of frequently used packages
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
[Python/Django] Summary of frequently used commands (3) <Operation of PostgreSQL>
Organize the meaning of methods, classes and objects
Full disclosure of methods used in machine learning
Smoothing of time series and waveform data 3 methods (smoothing)
Summary of frequently used Python arrays (for myself)
[Linux command] A memorandum of frequently used commands
[Python/Django] Summary of frequently used commands (2) <Installing packages>
Summary of frequently used commands (with petit commentary)
Beautiful Soup memo
Beautiful soup spills
phantomjs and selenium
[Introduction to Python] Summary of functions and methods that frequently appear in Python [Problem format]
I tried various things with Python: scraping (Beautiful Soup + Selenium + PhantomJS) and morphological analysis.
Save the text of all Evernote notes to SQLite using Beautiful Soup and SQLAlchemy
Note the frequently used options in Python + Selenium + Chrome
A collection of commands frequently used in server management
Summary of Pandas methods used when extracting data [Python]
Display a list of frequently used commands on Zsh
Delete / replace specific elements of HTML source [Beautiful Soup]
[Python] Types of statistical values (features) and calculation methods
[For beginners] Django Frequently used commands and reference collection