【问题标题】:Unable to scrape containers from webpages无法从网页中抓取容器
【发布时间】:2019-06-15 12:32:31
【问题描述】:

我正在尝试从电子商务网页练习网页抓取。我已将容器(包含每个产品的单元格)的类名标识为'c3e8SH'。然后,我使用以下代码抓取该网页中的所有容器。之后,我使用len(containers)查看网页中的容器数量。

但是,它返回了 0。有人可以指出我做错了什么吗?非常感谢!

import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.lazada.sg/catalog/?spm=a2o42.home.search.1.488d46b5mJGzEu&q=switch%20games&_keyori=ss&from=search_history&sugg=switch%20games_0_1'

# opening up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

#html parsing
page_soup = soup(page_html, 'html.parser')

#grabs each product
containers = page_soup.find_all('div', class_='c3e8SH')
len(containers)

【问题讨论】:

  • 你确定类名是'c3e8SH'吗?我尝试用正则表达式解决你的问题,但我找不到任何名称为“c3e8SH”的类。
  • @hamedbaziyad 我添加了一张照片,我引用错了吗?
  • 请确定你在图片中的部分。

标签: python web-scraping beautifulsoup urlopen


【解决方案1】:

(1) 首先,参数cookies is needed

如果您只请求链接without cookies,您将get the validation page如下@

https://www.lazada.sg/catalog/?spm=a2o42.home.search.1.488d46b5mJGzEu&q=switch%20games&_keyori=ss&from=search_history&sugg=switch%20games_0_1



(2)其次,你要抓取的页面是dynamicly loaded

这就是为什么你通过网络浏览器看到的与你通过代码看到的不同

为了方便,我更喜欢使用requests 模块。


import requests


my_url = 'https://www.lazada.sg/catalog/?spm=a2o42.home.search.1.488d46b5mJGzEu&q=switch%20games&_keyori=ss&from=search_history&sugg=switch%20games_0_1'


cookies = {
    "Hm_lvt_7cd4710f721b473263eed1f0840391b4":"1548133175,1548135160,1548135844",
    "Hm_lpvt_7cd4710f721b473263eed1f0840391b4":"1548135844",
    "x5sec":"7b22617365727665722d6c617a6164613b32223a223862623264333633343063393330376262313364633537653564393939303732434c50706d754946454e2b4b356f7231764b4c643841453d227d",
}

ret = requests.get(my_url, cookies=cookies)
print("New Super Mario Bros" in ret.text) # True 

# then you can get a json-style shop-items in ret.text  


商店物品如:

item_json = 

    {
        "@context":"https://schema.org",
        "@type":"ItemList",
        "itemListElement":[
            {
                "offers":{
                    "priceCurrency":"SGD",
                    "@type":"Offer",
                    "price":"72.90",
                    "availability":"https://schema.org/InStock"
                },
                "image":"https://sg-test-11.slatic.net/p/ae0494e8a5eb7412830ac9822984f67a.jpg",
                "@type":"Product",
                "name":"Nintendo Switch New Super Mario Bros U Deluxe",  # item name
                "url":"https://www.lazada.sg/products/nintendo-switch-new-super-mario-bros-u-deluxe-i292338164-s484601143.html?search=1"
            },
            ... 

        ]

    }

如 json 数据所示,您可以获取任何商品的名称、url-link、价格等。


【讨论】:

  • 对不起,我对此很陌生,我复制了您的代码(来自导入请求),但是,我返回的是 False。另外,你从哪里得到 cookie 下的代码字符串?如能不吝赐教,不胜感激!
  • @Daimon 您可以通过网络浏览器从request headers 找到它
【解决方案2】:

尝试使用不同的解析器。 我推荐lxml。 因此,您创建 page_soup 的行将是: page_soup = soup(page_html, 'lxml')

【讨论】:

    【解决方案3】:

    我尝试使用regex 在您建议的文档中找到c3e8SH,但我找不到这样的类名。请再次检查您的文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 2017-10-02
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 2019-12-07
      相关资源
      最近更新 更多