首页 > 解决方案 > Python 脚本在本地运行,但不在 Ubuntu 服务器上运行

问题描述

这是我在这个论坛上的第一篇文章,我希望以正确的方式解释我的问题。

所以我写了这个小的网络爬虫来更新我在亚马逊上的产品价格更新价格。之后,它会在 Telegram 上向我发送通知。

def check_price():
    page = requests.get(URL, headers=headers)

    soup = BeautifulSoup(page.content, 'html.parser')

    title = soup.find(id='priceblock_ourprice').get_text() # this is the problematic line
    converted_price = title[0:6]
    converted_price = float(converted_price.replace(',', '.'))

    if os.path.exists('data.txt'):
        with open('data.txt', 'r+') as f:
            f_contents = f.read()

            if converted_price != float(f_contents):
                send_msg('The price was updated to: ' + str(converted_price) + '€')
                f.write(str(converted_price))
    else:
        send_msg('The price was updated to: ' + str(converted_price) + '€')
        with open('data.txt', 'w') as f:
            f.write(str(converted_price))
    return

现在的问题是它可以在我的本地机器上运行并且我收到了通知。但是当我尝试在服务器上运行代码时,我收到以下消息:

Traceback (most recent call last):
   File "main.py", line 44, in <module>
      check_price()
   File "main.py", line 16, in check_price
      title = soup.find(id='priceblock_ourprice').get_text()
AttributeError: 'NoneType' object has no attribute 'get_text'

我只是发布用于检查价格而不是发送的主要功能,因为问题发生在此之前。我无法以我的方式找到错误。我希望你能帮助我,谢谢。

标签: pythonpython-3.xubuntuweb-crawler

解决方案


推荐阅读