【问题标题】:BeautifulSoup/Selenium: cannot find div, or any content in the bodyBeautifulSoup/Selenium:在正文中找不到 div 或任何内容
【发布时间】:2020-05-06 08:22:53
【问题描述】:

我的目标是在<div class="items-box-body"> 中获取商品名称(棉衬衫)和价格(¥3,600)

<div class="items-box-body">
    <h3 class="items-box-name font-2">cotton shirt</h3>
    <div class="items-box-num">
       <div class="items-box-price font-5">¥3,600</div>
    </div>
</div>

我使用了下面的代码,但无法访问任何div。当我测试soup.find_all() 时,我看不到body 之间的任何内容。

from bs4 import BeautifulSoup
from selenium import webdriver

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=r'C:\Users\...', chrome_options=options)
soup = BeautifulSoup(driver.page_source, "html.parser")

site_url = 'https://www.mercari.com/jp/search/?sort_order=&keyword=&category_root=1&category_child=11&category_grand_child%5B122%5D=1&brand_name=&brand_id=&size_group=&price_min=&price_max=&item_condition_id%5B1%5D=1&shipping_payer_id%5B2%5D=1&status_on_sale=1'
response = driver.get(site_url)
time.sleep(5)

print(soup.html.unwrap())
>> <html></html>

test = soup.find_all()
print('1',test)
>> [<head></head>, <body></body>]

body = soup.body()
print('2',body)
>> 2 []

for item in soup.select('div[class*="default-container "]'):
    print('3', item)
>>

for item in soup.select('div[class*="items-box-body"]'):
    print('4', item)
>>

我做错了什么?

超链接:https://www.mercari.com/jp/search/?...

【问题讨论】:

  • 可能是因为您试图在加载页面之前获取页面源?尝试在response = driver.get(site_url) 之后使用soup = BeautifulSoup(driver.page_source, "html.parser")。不是之前..

标签: python html selenium web-scraping beautifulsoup


【解决方案1】:

获取产品名称和价格。 诱导WebDriverWait() 并等待visibility_of_all_elements_located() 加载元素,然后将page_source 用于进一步处理。

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

site_url = 'https://www.mercari.com/jp/search/?sort_order=&keyword=&category_root=1&category_child=11&category_grand_child%5B122%5D=1&brand_name=&brand_id=&size_group=&price_min=&price_max=&item_condition_id%5B1%5D=1&shipping_payer_id%5B2%5D=1&status_on_sale=1'
driver.get(site_url)
WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,".items-box")))
html=driver.page_source
soup=BeautifulSoup(html,'html.parser')
for item in soup.select('.items-box'):
    print(item.find_next('h3',class_='items-box-name font-2').text.strip())
    print(item.find_next('div', class_='items-box-price font-5').text) 

你可以使用 python requests 模块在没有 selenium 的情况下做到这一点。

from bs4 import BeautifulSoup
import requests

headers = {'User-Agent':
       'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
res=requests.get("https://www.mercari.com/jp/search/?sort_order=&keyword=&category_root=1&category_child=11&category_grand_child%5B122%5D=1&brand_name=&brand_id=&size_group=&price_min=&price_max=&item_condition_id%5B1%5D=1&shipping_payer_id%5B2%5D=1&status_on_sale=1",headers=headers).text
soup=BeautifulSoup(res,'html.parser')
for item in soup.select('.items-box'):
    print(item.find_next('h3',class_='items-box-name font-2').text.strip())
    print(item.find_next('div', class_='items-box-price font-5').text)

【讨论】:

  • 非常感谢!请问为什么time.sleep() 在这种情况下不起作用? WebDriverWait()time.sleep() 有何不同?
  • @Jan : WebDriverWait() 等待预期的条件,即等待所有元素可见,一旦可见,它将移至下一条语句其中time.sleep() 等待特定时间,无论元素是否可见或不可见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
  • 1970-01-01
  • 2020-08-03
  • 2022-11-28
  • 2018-01-25
  • 1970-01-01
  • 2021-04-15
相关资源
最近更新 更多