超过时间限制如何解决?

问题描述 投票:0回答:1

导入请求 导入 json

导入系统 sys.path.insert(0,'bs4.zip') 从 bs4 导入 BeautifulSoup

模仿Mozilla浏览器。

user_agent = {'用户代理':'Mozilla/5.0'}

defcompare_prices(laughs_coconut_url, glomark_coconut_url): #TODO:获取包含产品价格的网页 smiles_response = requests.get(laughs_coconut_url, headers=user_agent) glomark_response = requests.get(glomark_coconut_url, headers=user_agent)

#TODO: LaughsSuper supermarket website provides the price in a span text.
soup_laughs = BeautifulSoup(laughs_response.text, 'html.parser')
price_laughs = soup_laughs.findAll('span', {'class': 'price'})[1].text.replace("Rs.", "").strip()
product_name_laughs = soup_laughs.find('h1').text.strip() if soup_laughs.find('h1') else "Unknown Product"

#TODO: Glomark supermarket website provides the data in jason format in an inline script.
#You can use the json module to extract only the price
soup_glomark = BeautifulSoup(glomark_response.text, 'html.parser')
data_glomark = json.loads(soup_glomark.find('script', {'type': 'application/ld+json'}).text)
price_glomark = float(data_glomark['offers'][0]['price'])
product_name_glomark = data_glomark['name']

#TODO: Parse the values as floats, and print them
price_laughs = float(price_laughs)


print(f'Laughs   {product_name_laughs} Rs.: {price_laughs}')
print(f'Glomark  {product_name_glomark} Rs.: {price_glomark}')


if price_laughs > price_glomark:
    print(f'Glomark is cheaper by Rs.: {price_laughs - price_glomark}')
elif price_laughs < price_glomark:
    print(f'Laughs is cheaper by Rs.: {price_glomark - price_laughs}')
else:
    print('Prices are the same')

要比较的网址

laughs_coconut_url = 'https://scrape-sm1.github.io/site1/COCONUT%20market1super.html' glomark_coconut_url = 'https://glomark.lk/coconut/p/11624' 比较价格(laughs_coconut_url,glomark_coconut_url)

我明白了超出时间限制

time compare limit
1个回答
0
投票

您使用的是什么IDE?

超过时间限制当控制台命令(在我们的例子中 - python3 main.py)休眠时间超过系统时间限制时,会引发错误。要增加命令时间,请使用

timeout TIME python3 main.py
,其中 TIME 是等待的分钟数。

在您的代码中,由于

requests.get()
函数,可能会发生这种情况。您的网址可能会运行得很慢。

© www.soinside.com 2019 - 2024. All rights reserved.