“Mengikis web dengan selenium” Kode Jawaban

Contoh Mengikis Web Python Selenium

#Python example - use chrome driver to open google url and enter into search bar "Why is python so awesome"

#required imports
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

#open driver
PATH_TO_DRIVER = './chromedriver'
driver = webdriver.Chrome(executable_path=PATH_TO_DRIVER)

#launch url using driver
driver.get('https://google.com')

#find element to manipulate e.g. by element name. 'q' is the element name of the google search bar
element = driver.find_element_by_name('q')

#write text into search bar
element.send_keys('Why is python so awesome')

#simulates 'Enter' key
element.send_keys(Keys.ENTER)
Fun Bee

Mengikis web dengan selenium

from bs4 import BeautifulSoup
from selenium import webdriver
 
option = webdriver.ChromeOptions()
# I use the following options as my machine is a window subsystem linux. 
# I recommend to use the headless option at least, out of the 3
option.add_argument('--headless')
option.add_argument('--no-sandbox')
option.add_argument('--disable-dev-sh-usage')
# Replace YOUR-PATH-TO-CHROMEDRIVER with your chromedriver location
driver = webdriver.Chrome('YOUR-PATH-TO-CHROMEDRIVER', options=option)
 
driver.get('https://www.imdb.com/chart/top/') # Getting page HTML through request
soup = BeautifulSoup(driver.page_source, 'html.parser') # Parsing content using beautifulsoup. Notice driver.page_source instead of page.content
 
links = soup.select("table tbody tr td.titleColumn a") # Selecting all of the anchors with titles
first10 = links[:10] # Keep only the first 10 anchors
for anchor in first10:
    print(anchor.text) # Display the innerText of each anchor
Ramon Hernandez

Jawaban yang mirip dengan “Mengikis web dengan selenium”

Pertanyaan yang mirip dengan “Mengikis web dengan selenium”

Lebih banyak jawaban terkait untuk “Mengikis web dengan selenium” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya