【问题标题】:Error scraping Amazon Data with Python and BeautifulSoup使用 Python 和 BeautifulSoup 抓取 Amazon 数据时出错
【发布时间】:2019-07-10 20:12:44
【问题描述】:

我刚开始使用 Python,我有一个奇怪的行为,Python 大部分时间都会给我一个错误,有时它会正确编译我的代码。

import requests
from bs4 import BeautifulSoup

jblCharge4URL = 'https://www.amazon.de/JBL-Charge-Bluetooth-Lautsprecher-Schwarz-integrierter/dp/B07HGHRYCY/ref=sr_1_2_sspa?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&keywords=jbl+charge+4&qid=1562775856&s=gateway&sr=8-2-spons&psc=1'

def get_page(url):
    page = requests.get(url, headers=headers)
    soup = BeautifulSoup(page.content, 'html.parser')
    return soup

def get_product_name(url):
    soup = get_page(url)
    try:
        title = soup.find(id="productTitle").get_text()
        print("SUCCESS")
    except AttributeError:
        print("ERROR")
while(True)
    print(get_product_name(jblCharge4URL))

控制台输出:

ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
**SUCCESS**  
None  
ERROR  
None  
**SUCCESS**  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None  
ERROR  
None

提前致谢

【问题讨论】:

    标签: python python-3.x beautifulsoup python-requests


    【解决方案1】:

    我对您的代码进行了一些调整,这应该会让您回到正确的轨道:

    import requests
    from bs4 import BeautifulSoup
    
    jblCharge4URL = 'https://www.amazon.de/JBL-Charge-Bluetooth-Lautsprecher-Schwarz-      integrierter/dp/B07HGHRYCY/ref=sr_1_2_sspa?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&  keywords=jbl+charge+4&qid=1562775856&s=gateway&sr=8-2-spons&psc=1'
    
    def get_page(url):
        page = requests.get(url)
        soup = BeautifulSoup(page.text, 'html.parser')
        return soup
    
    def get_product_name(url):
        soup = get_page(url)
        try:
            title = soup.find(id="productTitle")
            print("SUCCESS")
    
        except AttributeError:
            print("ERROR")
        return(title)   
    print(get_product_name(jblCharge4URL))
    

    【讨论】:

      【解决方案2】:

      你在page = requests.get(url, headers=headers) 中使用的是什么headers? 你会想要一些东西来欺骗服务器,让他们相信你是一个真正的用户而不是一个脚本。我建议使用一些基本的东西,比如

      headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'}
      

      此外,您可能希望在调试此问题时在异常中打印变量 soup 的值。打印 soup 将为您提供页面的 HTML,然后您可以深入研究源代码以了解问题所在。

      【讨论】:

        【解决方案3】:

        除了使用requestsBeautifulSoup 组合之外,您还可以使用requests-html 包下载您的网页并同时解析内容。使用 requests-html 的一个例子是:

        from requests_html import HTMLSession
        
        url = r"https://www.amazon.de/JBL-Charge-Bluetooth-Lautsprecher-Schwarz-integrierter/dp/B07HGHRYCY/"
        
        req = HTMLSession().get(url)
        product_title = req.html.find("#productTitle", first=True)
        print(product_title.text)  #JBL Charge 4 Bluetooth-Lautsprecher in Schwarz – Wasserfeste, portable Boombox mit integrierter Powerbank – Mit nur einer Akku-Ladung bis zu 20 Stunden kabellos Musik streamen
        

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-03
          • 1970-01-01
          • 1970-01-01
          • 2020-12-22
          • 1970-01-01
          • 2021-12-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多