【问题标题】:BeautifulSoup: Why .select method returned an empty list?BeautifulSoup:为什么 .select 方法返回一个空列表?
【发布时间】:2020-11-10 05:43:31
【问题描述】:

我想用 BeautifulSoup 模拟“点击”动作,以便抓取返回的页面。我尝试了 selenium webdriver 和 BeautifulSoup,但每次都得到一个空列表。在下面的代码中,我复制了选择器——我的最后一次尝试,但它仍然不起作用。

# Scraping top products sales and name from the Recommendation page

from selenium import webdriver
from bs4 import BeautifulSoup as bs
import json
import requests
import numpy as np
import pandas as pd

headers = {
    'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36',
    'cookie': '_gcl_au=1.1.961206468.1594951946; _med=refer; _fbp=fb.2.1594951949275.1940955365; SPC_IA=-1; SPC_F=y1evilme0ImdfEmNWEc08bul3d8toc33; REC_T_ID=fab983c8-c7d2-11ea-a977-ccbbfe23657a; SPC_SI=uv1y64sfvhx3w6dir503ixw89ve2ixt4; _gid=GA1.3.413262278.1594951963; SPC_U=286107140; SPC_EC=GwoQmu7TiknULYXKODlEi5vEgjawyqNcpIWQjoxjQEW2yJ3H/jsB1Pw9iCgGRGYFfAkT/Ej00ruDcf7DHjg4eNGWbCG+0uXcKb7bqLDcn+A2hEl1XMtj1FCCIES7k17xoVdYW1tGg0qaXnSz0/Uf3iaEIIk7Q9rqsnT+COWVg8Y=; csrftoken=5MdKKnZH5boQXpaAza1kOVLRFBjx1eij; welcomePkgShown=true; _ga=GA1.1.1693450966.1594951955; _dc_gtm_UA-61904553-8=1; REC_MD_30_2002454304=1595153616; _ga_SW6D8G0HXK=GS1.1.1595152099.14.1.1595153019.0; REC_MD_41_1000044=1595153318_0_50_0_49; SPC_R_T_ID="Am9bCo3cc3Jno2mV5RDkLJIVsbIWEDTC6ezJknXdVVRfxlQRoGDcya57fIQsioFKZWhP8/9PAGhldR0L/efzcrKONe62GAzvsztkZHfAl0I="; SPC_T_IV="IETR5YkWloW3OcKf80c6RQ=="; SPC_R_T_IV="IETR5YkWloW3OcKf80c6RQ=="; SPC_T_ID="Am9bCo3cc3Jno2mV5RDkLJIVsbIWEDTC6ezJknXdVVRfxlQRoGDcya57fIQsioFKZWhP8/9PAGhldR0L/efzcrKONe62GAzvsztkZHfAl0I="'
}
shopee_url = 'https://shopee.co.id/top_products'

navi_info = requests.get('https://shopee.co.id/api/v4/recommend/recommend?bundle=top_sold_product_microsite&limit=20&offset=0')
# extracts all the "index" data from all "sections"
index_arrays = [object_['index'] for object_ in navi_info.json()['data']['sections']]
index_array = index_arrays[0] # only one section with "index" key is present
# extract all catIDs from the "index" payload
catIDs = [object_['key'] for object_ in index_array]
params = {'catID': catIDs}
print(params)

# a = requests.get(link, headers=headers)
response = requests.get('https://shopee.co.id/top_products', params=params)
soup = bs(response.text, 'html.parser')
products = soup.select('#main > div > div.shopee-page-wrapper > div._3b-UMP.container > div > div.stardust-tabs-panels > section:nth-child(1) > div > div:nth-child(1) > a')
print(products) # Why this returns an empty list? 
for product in products:
    name = product.select_one('#main > div > div.shopee-page-wrapper > div.page-product > div.container > div.product-briefing.flex.card._2cRTS4 > div.flex.flex-auto.k-mj2F > div > div.qaNIZv > span')
    sales = product.select_one('#main > div > div.shopee-page-wrapper > div.page-product > div.container > div.product-briefing.flex.card._2cRTS4 > div.flex.flex-auto.k-mj2F > div > div.flex._32fuIU > div.flex.SbDIui > div._22sp0A')
    print(name)
    print(sales)

【问题讨论】:

    标签: python html css beautifulsoup web-crawler


    【解决方案1】:

    我的猜测是您在 css 选择器链中包含构建工件。

    div._3b-UMP.containerdiv.product-briefing.flex.card._2cRTS4。它们可能在构建和会话中不一致。您需要重写选择器以不使用任何构建工件

    【讨论】:

    • 你能说得更具体点吗?因为我对前端知识不是很熟悉,昨天才开始学习爬虫。非常感谢。
    • 由于您是 Web 开发新手,因此您需要了解 css 选择器:developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors css 选择器是您为 select() 和 select_one() 函数指定 html 元素的方式。您当前的选择器非常具体。 > 表示直接子代,因此您正在查看 id = main、div 的子代、shopee-page-wrapper 类的 div 的子代等。您需要找到一个选择器来选择您在相关页面上感兴趣的元素。
    • 另外请记住,由于听起来您不是页面的所有者,因此页面结构可能会立即更改并破坏您的脚本,就像网络爬虫的问题一样一般。
    • 我去查看源页面,我猜我得到一个空列表的原因是源页面中没有'div'。如果我是对的,那么我该如何抓取我正在寻找的内容?
    • 您必须检查 html 源代码并找出标签和类中的一致模式。由于您正在查看的此页面是动态生成的,因此由于延迟加载内容,可能有各种方式使抓取工具无法轻松访问内容。要么您必须使用较长的超时时间来等待所有页面资源呈现和解析,要么您必须观看任何 ajax 查询并等待它们解决,这在轮询的情况下不起作用。
    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多