python - 抓取亚马逊交易页面不返回 html 代码 - python

标签 python web-scraping beautifulsoup python-requests

我目前正在尝试使用以下代码抓取此亚马逊页面“https://www.amazon.com/b/?ie=UTF8&node=11552285011&ref_=sv_kstore_5 ”:

from bs4 import BeautifulSoup
import requests
url = 'https://www.amazon.com/b/?ie=UTF8&node=11552285011&ref_=sv_kstore_5'
r = requests.get(url)
soup = BeautifulSoup(r.content)
print(soup.prettify)

但是,当我运行它而不是获取简单的 html 源代码时,我得到了一堆对我来说没有多大意义的行,如下所示:

<bound method Tag.prettify of <!DOCTYPE html>
<html class="a-no-js" data-19ax5a9jf="dingo"><head><script>var aPageStart = (new Date()).getTime();</script><meta charset="utf-8"/><!--  emit CSM JS -->
<style>
[class*=scx-line-clamp-]{overflow:hidden}.scx-offscreen-truncate{position:relative;left:-1000000px}.scx-line-clamp-1{max-height:16.75px}.scx-truncate-medium.scx-line-clamp-1{max-height:20.34px}.scx-truncate-small.scx-line-clamp-1{max-height:13px}.scx-line-clamp-2{max-height:35.5px}.scx-truncate-medium.scx-line-clamp-2{max-height:41.67px}.scx-truncate-small.scx-line-clamp-2{max-height:28px}.scx-line-clamp-3{max-height:54.25px}.scx-truncate-medium.scx-line-clamp-3{max-height:63.01px}.scx-truncate-small.scx-line-clamp-3{max-height:43px}.scx-line-clamp-4{max-height:73px}.scx-truncate-medium.scx-line-clamp-4{max-height:84.34px}.scx-truncate-small.scx-line-clamp-4{max-height:58px}.scx-line-clamp-5{max-height:91.75px}.scx-truncate-medium.scx-line-clamp-5{max-height:105.68px}.scx-truncate-small.scx-line-clamp-5{max-height:73px}.scx-line-clamp-6{max-height:110.5px}.scx-truncate-medium.scx-line-clamp-6{max-height:127.01

即使当我向下滚动时,也没有任何东西真正类似于包含我需要的所有信息的结构化 html 代码。我究竟做错了什么 ? (我是一个初学者,所以它可以是任何东西)。非常感谢!

最佳答案

print(soup.prettify)

打算调用soup.prettify.__repr__()。输出为

<bound method Tag.prettify of <!DOCTYPE html><html class="a-no-js" data-19ax5a9jf="dingo"><head>...

当您需要调用prettify方法时:

print(soup.prettify())

输出:

<html class="a-no-js" data-19ax5a9jf="dingo">
 <head>
  <script>
   var aPageStart = (new Date()).getTime();
  </script>
  <meta charset="utf-8"/>
  <!--  emit CSM JS -->
  <style>
  ...

关于python - 抓取亚马逊交易页面不返回 html 代码 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53414246/

相关文章:

python - 基于Enum迭代或索引列表

javascript - 如何使用 Node.js 抓取页面

html - 刮刀 : distinguishing meaningful text from meaningless items, hadoop

python - Python 中的 BeautifulSoup - 获取类型的第 n 个标签

python - 如何在具有多个表的网页上调用特定表

python - 在 HTML 中显示一个 python 文件,保留空格等

python - 如何使用Python3将ruamel.yaml转换为dict?

python - 以更有吸引力的方式打印字典键

python - @src 图像属性的 Xpath

python - 使用 BeautifulSoup 将数据抓取到数据框中