【问题标题】:Locating an element in bs4在 bs4 中定位元素
【发布时间】:2019-03-01 06:03:36
【问题描述】:

试图在此页面上抓取每个项目推土机的所有信息。 我刚刚开始,对抓取只有相当的想法,但不知道该怎么做。

driver=webdriver.Firefox()
driver.get('https://www.rbauction.com/dozers?keywords=&category=21261693092')    
soup=BeautifulSoup(driver.page_source,'html.parser')

#trying all d/f ways buh getting oly nonetype or no element
get= soup.findAll('div' , attrs={'class' : 'sc-gisBJw eHFfwj'})
get2= soup.findAll('div' , attrs={'id' : 'searchResultsList'})
get3= soup.find('div.searchResultsList').find_all('a')

我必须进入每个类/id 并循环 a['href'] 并获取每个推土机的信息。 请帮忙。

【问题讨论】:

  • 您面临的问题是什么
  • @Preethi 你能用你正在寻找的此页面上每个项目推土机的确切信息更新问题吗?请edit the question 将其限制为具有足够详细信息的特定问题,以确定适当的答案。避免一次问多个不同的问题。请参阅How to Ask 页面以获得澄清此问题的帮助。

标签: python selenium beautifulsoup


【解决方案1】:

您需要等待要加载的数据才能将其读入 BeautifulSoup 对象。在 selenium 中使用 WebDriverWait 等待页面加载,因为它需要一段时间才能完全呈现:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('https://www.rbauction.com/dozers?keywords=&category=21261693092')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'searchResultsList')))
soup = BeautifulSoup(driver.page_source,'html.parser')

这行应该从页面返回href:

hrefs = [el.attrs.get('href') for el in soup.find('div', attrs={'id': 'searchResultsList'}).find_all('a')]

【讨论】:

  • 您好,感谢您的帮助!但这给了我列表中的所有链接。我实际上想进入每个链接(all_links)并带来其产品详细信息。不过这很有帮助。请帮忙
  • for loop in all_links: loop.click() break 我获取每个链接并单击该链接获取产品信息。
【解决方案2】:

你可以只使用请求

import requests 
headers = {'Referrer':'https://www.rbauction.com/dozers?keywords=&category=21261693092'}
data = requests.get('https://www.rbauction.com/rba-msapi/search?keywords=&searchParams=%7B%22category%22%3A%2221261693092%22%7D&page=0&maxCount=48&trackingType=2&withResults=true&withFacets=true&withBreadcrumbs=true&catalog=ci&locale=en_US', headers = headers).json()

for item in data['response']['results']:
    print(item['name'],item['url'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2020-06-22
    • 2019-12-19
    相关资源
    最近更新 更多