如何用 Pythony 语言写一个在京东商城抢购 618 特价商品的程序

查看 13|回复 0
作者:pauluswanggang   
下面是我在 Chatgpt 上得到的代码程,我是个初学者,这个程序这样写有问题吗?
import requests
from bs4 import BeautifulSoup
import time
# 京东的商品 URL
url = 'https://item.jd.com/100012043978.html'
# 设置请求头,模拟浏览器
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'
}
def check_price(url):
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')

# 解析价格 (需要根据具体页面结构调整)
price_tag = soup.find('span', {'class': 'price'})
if price_tag:
price = price_tag.text.strip()
print(f"当前价格: {price}")
else:
print("未能获取价格信息")
def main():
while True:
check_price(url)
# 等待一段时间后再次检查,避免频繁请求
time.sleep(60) # 每 60 秒检查一次
if __name__ == "__main__":
main()
您需要登录后才可以回帖 登录 | 立即注册

返回顶部