Home > Web Front-end > JS Tutorial > body text

Guide to reading web page elements

WBOY
Release: 2024-04-09 12:39:02
Original
1177 people have browsed it

To read web page elements using Python, follow these steps: Import the webdriver from the Selenium library. Launch a browser such as Chrome Driver. Use the find_element_by_* methods to find web page elements. Use element.text to read element text. Use element.get_attribute() to read element attributes. Use element.location and element.size to read element position and size.

Guide to reading web page elements

Web page element reading guide

Web page element reading is a key task for website automation and data extraction. This article will guide you how to read the text, attributes and position of web page elements using Python and Selenium.

Import the necessary libraries

from selenium import webdriver
Copy after login

Start the browser

driver = webdriver.Chrome()  # 或其他浏览器驱动程序
Copy after login

Find web page elements

Find the element using Selenium's find_element_by_* method:

  • find_element_by_id("my_id")
  • find_element_by_name ("my_name")
  • find_element_by_class_name("my_class")
  • find_element_by_xpath("//element/path")

Read element text

text = element.text
Copy after login

Read element attribute

value = element.get_attribute("attribute_name")
Copy after login

Read element position

location = element.location  # 返回 {x, y} 坐标
size = element.size  # 返回 {width, height}
Copy after login

Practical example

Extract movie titles and ratings from the IMDb website:

# 打开 IMDb 网站
driver.get("https://www.imdb.com/")

# 获取前 10 部电影的标题和评分
titles = []
ratings = []
for i in range(1, 11):
    # 查找标题元素
    title_element = driver.find_element_by_xpath(f"(//h3)[{i}]/a")
    # 读标题
    title = title_element.text

    # 查找评分元素
    rating_element = driver.find_element_by_xpath(f"(//strong)[{i}]")
    # 读评分
    rating = rating_element.text

    titles.append(title)
    ratings.append(rating)

# 打印结果
for title, rating in zip(titles, ratings):
    print(f"{title}: {rating}")
Copy after login

This will print results similar to the following:

The Shawshank Redemption: 9.3
The Godfather: 9.2
The Dark Knight: 9.0
Schindler's List: 9.0
12 Angry Men: 9.0
...
Copy after login

The above is the detailed content of Guide to reading web page elements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template