提取链接中提到的网站标题

huangapple go评论63阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年2月16日 13:14:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75468097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定