Python BeautifulSoup - 无法读取网站分页

标签 python web-scraping beautifulsoup

我尝试使用包含网站分页的 class='no-selected-number extreme-number' 提取 div,但我没有得到预期的结果.谁能帮帮我?

下面是我的代码:

import requests  from bs4 import BeautifulSoup 

URL ="https://www.falabella.com.pe/falabella-pe/category/cat40703/Perfumes-de-Mujer/"
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538 Safari/537.36'}

r = requests.get(URL, headers=headers, timeout=5)  html = r.content

soup = BeautifulSoup(html, 'lxml')  box_3 =
soup.find_all('div','fb-filters-sort') 
for div in box_3:
  last_page = div.find_all("div",{"class","no-selected-number extreme-number"})
  print(last_page)

最佳答案

您可能需要一种允许页面加载时间的方法,例如使用 Selenium 。我认为您要获取的数据不会出现在 requests 中。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options  

chrome_options = Options()  
chrome_options.add_argument("--headless")  
url ="https://www.falabella.com.pe/falabella-pe/category/cat40703/Perfumes-de-Mujer/"
d = webdriver.Chrome(chrome_options=chrome_options)
d.get(url)
print(d.find_element_by_css_selector('.content-items-number-list .no-selected-number.extreme-number:last-child').text)
d.quit()

关于Python BeautifulSoup - 无法读取网站分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53340307/

相关文章:

javascript - 将元素添加到愿望 list | Django

python - 无法使用请求连接到 Tor,而我使用 selenium 也做了同样的事情

javascript - 是否可以使用 Ruby 和 Nokogiri 插入 JavaScript 引擎?

PYTHON - BEAUTIFULSOUP 如何将空 TD(表数据)刮取为空值而不是跳过它

python - 使用 from_dict 在 pandas 中添加而不是附加 NaN

python - 重新排列字符串/列表的所有方法的列表

python - NumPy 数组和 Python 列表有什么区别?

google-sheets - 如何在 Google 表格中一起使用 IMPORTXML 和 SEQUENCE

python - 如何在 BeautifulSoup 中找到 `td` 父标签的所有 `tr` 且具有 `class="Accordion-toggle main-row"' 的父标签?

Python BeautifulSoup 从 header 中提取数据