Чтобы сохранить данные с веб-страницы в список с помощью Python, можно использовать несколько методов. Вот несколько примеров:
Метод 1. Использование запросов и библиотек BeautifulSoup
import requests
from bs4 import BeautifulSoup
url = "https://example.com" # Replace with the URL of the webpage you want to scrape
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# Find the relevant elements on the webpage and save them into a list
data_list = []
for element in soup.find_all("input"):
data_list.append(element["value"])
print(data_list)
Метод 2: использование Selenium WebDriver
from selenium import webdriver
url = "https://example.com" # Replace with the URL of the webpage you want to scrape
# Configure Selenium WebDriver (make sure you have the appropriate driver installed)
driver = webdriver.Chrome() # Replace with the appropriate driver for your browser
driver.get(url)
# Find the relevant elements on the webpage and save them into a list
data_list = []
elements = driver.find_elements_by_tag_name("input")
for element in elements:
data_list.append(element.get_attribute("value"))
driver.quit()
print(data_list)
Метод 3. Использование платформы Scrapy
import scrapy
class MySpider(scrapy.Spider):
name = "my_spider"
start_urls = ["https://example.com"] # Replace with the URL of the webpage you want to scrape
def parse(self, response):
# Find the relevant elements on the webpage and save them into a list
data_list = response.css("input::attr(value)").getall()
print(data_list)
# Run the spider
process = scrapy.crawler.CrawlerProcess()
process.crawl(MySpider)
process.start()
Это всего лишь несколько примеров того, как можно сохранить данные с веб-страницы в список с помощью Python. Выбор метода будет зависеть от конкретных требований вашего проекта.