python - 无法抓取此网站。如何从该站点抓取数据?
问题描述
我无法从该站点抓取数据。
我在其他网站上试过,但在其他网站上没问题...
from bs4 import BeautifulSoup
from urllib.request import urlopen
response = urlopen("https://www.daraz.com.np/catalog/?spm=a2a0e.searchlistcategory.search.2.3eac4b8amQJ0zd&q=samsung%20m20&_keyori=ss&from=suggest_normal&sugg=samsung%20m20_1_1")
html = response.read()
parsed_html = BeautifulSoup(html, "html.parser")
containers = parsed_html.find_all("div", {"class" : "c2prKC"})
print(len(containers))
解决方案
看起来像 JS 加载后渲染到页面。您可以使用 Selenium 渲染页面和美丽的汤来获取元素。
from bs4 import BeautifulSoup
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.daraz.com.np/catalog/?spm=a2a0e.searchlistcategory.search.2.3eac4b8amQJ0zd&q=samsung%20m20&_keyori=ss&from=suggest_normal&sugg=samsung%20m20_1_1")
time.sleep(5)
html = driver.page_source
parsed_html = BeautifulSoup(html, "html.parser")
containers = parsed_html.find_all("div", {"class" : "c2prKC"})
print(len(containers))
推荐阅读
- google-cloud-platform - 尝试通过 gulp-gcloud-publish 包将编译后的 CSS 发布到 GCP 存储桶时出现“TypeError:file.pipe 不是函数”
- laravel - Livewire:如何获取新创建行的 ID?
- asp.net - 如果从 [Authorized] 重定向到帐户/登录,则 oauth 的 CORS 问题
- .htaccess - 使用 htaccess 删除尾随 /connect 字符串(301 重定向)
- pine-script - Pine Script - 有没有办法及时找到价格,但只需根据该变量在价格上划一条水平线
- python - 如何从 Azure Blob 存储下载图像并将其存储到本地目录中?
- ios - 如何在 Xcode 12.4 上运行具有控制中心的 iOS 模拟器?
- python - 如何修改networkx有向图的外观并向边缘添加权重
- go - 如何动态添加列到 container.NewGridWithColumns()
- c++ - 使用 C++ 验证数字