英文:
Extracting the titles of the websites mentioned in the link
问题
import requests
from bs4 import BeautifulSoup
# 发送GET请求并解析HTML内容
url = 'https://www.g2.com/categories/marketing-automation'
response = requests.get(url).text
soup = BeautifulSoup(response, 'html.parser')
name = soup.find(class_="product-card__product-name")
print(name)
上面的代码是一个测试代码,用于检查是否成功获取数据,但返回的结果是None
。
英文:
https://www.g2.com/categories/marketing-automation
I am trying webscrap the above link that has list of 350+ websites i need to extract the title of the websites mentioned
But I am failing to get any results i have tried with using requests and beautiful soup
then with selenium and all i am getting is empty list "[]" or none
import requests
from bs4 import BeautifulSoup
# Send a GET request to the URL and parse the HTML content
url = 'https://www.g2.com/categories/marketing-automation'
response = requests.get(url).text
soup = BeautifulSoup(response, 'html.parser')
name = soup.find(class_ = "product-card__product-name")
print(name)
This above code is just a test code to check if the data is being pulled or not and the response is 'None'
From this code i am expecting to see the results of the class mentioned upon calling print
答案1
得分: 1
from selenium import webdriver
from selenium.webdriver.common.by import By
# 创建一个新的Chrome驱动实例
driver = webdriver.Chrome()
# 导航到网页
driver.get('https://www.g2.com/categories/marketing-automation')
# 等待页面加载
driver.implicitly_wait(10)
# 查找页面上的所有产品卡片
product_cards = driver.find_elements(By.CLASS_NAME, 'product-card__product-name')
# 遍历产品卡片并提取每个卡片的标题
for product_card in product_cards:
title = product_card.text
print(title)
# 关闭浏览器
driver.quit()
英文:
I kind of got this code to get something. Im still working on it.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Create a new instance of the Chrome driver
driver = webdriver.Chrome()
# Navigate to the webpage
driver.get('https://www.g2.com/categories/marketing-automation')
# Wait for the page to load
driver.implicitly_wait(10)
# Find all the product cards on the page
product_cards = driver.find_elements(By.CLASS_NAME, 'product-card__product-name')
# Iterate over the product cards and extract the title from each one
for product_card in product_cards:
title = product_card.text
print(title)
# Close the browser
driver.quit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论