【问题标题】:Is there someone have success in scraping from Amazon using Beautifulsoup?有人使用 Beautifulsoup 从亚马逊抓取成功吗?
【发布时间】:2022-01-18 00:32:48
【问题描述】:

我想做一个亚马逊的网络爬虫。

但是,everydata 好像是 None 类型。

我在 google 上发现,有很多人在制作亚马逊的网络爬虫。

请给我一些建议来解决这个 Nonetype 问题。

这是我的代码:

import requests
from bs4 import BeautifulSoup

amazon_dir = requests.get("https://www.amazon.es/s?k=docking+station&__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=34FO3BVVCJS4V&sprefix=docking%2Caps%2C302&ref=nb_sb_ss_ts-doa-p_1_7")
amazon_soup = BeautifulSoup(amazon_dir.text, "html.parser")
product_table = amazon_soup.find("div", {"class": "sg-col-inner"})
print(product_table)

products = product_table.find("div", {"class": "a-section"})
name = products.find("span", {"class": "a-size-base-plus"})
rating = products.find("span", {"class": "a-icon-alt"})
price = products.find("span", {"class": "a-price-whole"})
print(name, rating, price)

谢谢

【问题讨论】:

  • 先查看响应码,尝试添加用户代理!检查网站是否使用JS。
  • @αԋɱҽԃαмєяιcαη 我从不这样做。但是,我会找到并尝试一下。谢谢
  • 如果amazon.es 使用JavaScript 那么requests + BeautifulSoup 将不起作用,因为它们无法运行JavaScript。我不确定,但对于某些国家/地区Amazon 可能使用JavaScript,而对于其他国家/地区它可能在没有JavaScript 的情况下运行。或者也许很久以前Amazan 在没有JavaScript 的情况下运行,但现在使用JavaScript - 你应该检查互联网上的教程/帖子的年龄。
  • 如果我使用标题 User-Agent: Mozilla/5.0,代码对我有用
  • @furas 非常感谢。不适用于这种情况,但是我如何知道该网站是否使用 JavaScript 运行?我想知道

标签: python web-scraping beautifulsoup amazon


【解决方案1】:

门户可能会检查标头 User-Agent 以针对不同的浏览器或设备发送不同的 HTML,有时这会导致在页面上查找元素时出现问题。

但通常门户网站会检查此标头以阻止脚本/机器人。
例如requests 发送User-Agent: python-requests/2.26.0

如果我使用来自真实浏览器的标题 User-Agent 或至少更短的版本 Mozilla/5.0,那么代码可以工作。


还有其他问题。

<div class="sg-col-inner" ...> 几乎有 70 个元素,table 是第三个元素,但 find() 只给出第一个元素。您必须先使用find_all(),然后再使用[2] 才能获得第三个元素。


import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0',
}    

url = "https://www.amazon.es/s?k=docking+station&__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=34FO3BVVCJS4V&sprefix=docking%2Caps%2C302&ref=nb_sb_ss_ts-doa-p_1_7"
response = requests.get(url, headers=headers)

print(response.text[:1000])
print('---')

amazon_soup = BeautifulSoup(response.text, "html.parser")
all_divs = amazon_soup.find_all("div", {"class": "sg-col-inner"})

print('len(all_divs):', len(all_divs))
print('---')

products = all_divs[3].find("div", {"class": "a-section"})
name = products.find("span", {"class": "a-size-base-plus"})
rating = products.find("span", {"class": "a-icon-alt"})
price = products.find("span", {"class": "a-price-whole"})
print('name:', name.text)
print('rating:', rating.text)
print('price:', price.text)

编辑:

显示所有产品的版本:

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0',
}    

url = "https://www.amazon.es/s?k=docking+station&__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&crid=34FO3BVVCJS4V&sprefix=docking%2Caps%2C302&ref=nb_sb_ss_ts-doa-p_1_7"
response = requests.get(url, headers=headers)

#print(response.text[:1000])
#print('---')

soup = BeautifulSoup(response.text, "html.parser")

results = soup.find("div", {"class": "s-main-slot s-result-list s-search-results sg-row"})

all_products = results.find_all("div", {"class": "sg-col-inner"})
print('len(all_products):', len(all_products))
print('---')

for item in all_products:
    name = item.find("span", {"class": "a-size-base-plus"})
    rating = item.find("span", {"class": "a-icon-alt"})
    price = item.find("span", {"class": "a-price-whole"})
    if name:
        print('name:', name.text)
    if rating:
        print('rating:', rating.text)
    if price:
        print('price:', price.text)
    if name or rating or price:
        print('---')

顺便说一句:

门户网站会不时刷新服务器上的代码和 HTML - 因此,如果您找到教程,请检查它的年龄。较旧的教程可能无法使用,因为门户网站可能会更改代码中的某些内容。

许多现代页面开始使用 JavaScript 添加元素,但 requestsBeautifulSoup 无法运行 JavaScript。而这可能需要使用Selenium来控制可以运行JavaScript的真实网络浏览器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多