I will give you a crawling commodity price curve based on Python, I wish you a happy shopping

sparse past

Introduce yourself first! A native of Langfang, Hebei, Hubei University of Technology (college civil engineering), after 20 years of graduation, he returned to his hometown Langfang to work as a major. Tell me why you want to change careers after graduation! I also know several senior brothers in the industry. Compared with my friends who do it, there is also a big gap in their work in 45 years. When I got in touch, I found that I didn't love civil engineering But good employment is real. I started working as soon as I graduated, with a salary of 2500. In a private enterprise construction unit, my physical labor is greater than my mental labor. To put it bluntly, I am a migrant worker with glasses. Fortunately, I wear glasses, otherwise I am a migrant worker. During that time, there was a girl I liked that I didn’t have the confidence to pursue, she was sloppy, she had to smoke and drink, she overdrafted her body, and the salary increase was very low. I have basically seen the salary of 5,000 senior brothers who have worked for 5 years, and the one wh o can see the future at a glance. I am the most terrifying, the prospect is far away, and at the end of the 20th year, there are often constructions in the middle of the night, and overtime is often the case, mainly because the party A is your uncle. A person, just do their job well, which is exactly what this industry needs. The old buddies who started the industry earlier than me, we often have supper at night, and I feel that they are all drinking to numbus, and the age and capital of changing careers are too late. Programmers also work overtime, which is comforting compared to subsidies. There are many more feelings above, of course, it does not rule out a good senior brother who is very fond of civil engineering.

Not much experience, too many sighs, too many feelings, I don't blame the industry, only myself. At that time, the only idea was to jump out early, and I couldn't jump out later. Brother Li, who I worked with at that time, said many of them also support me in changing careers, don't follow in my own footsteps, even if I fail to change careers I am also willing to thank them for their support.

It took almost a whole year from the start of changing careers to finding a job. The time can be said to be fast or slow. Finally, I chose to learn Python, not for other reasons, just because it is easy to use and the salary is considerable!

Attached is a learning picture. At that time, I was still typing the code at 12:00 in the morning. At that time, the circle of friends that I cheered on was not deleted. Found the only picture.

618 gifts for everyone

The annual 618 shopping festival is coming, and various promotions have flooded Taobao, JD.com, Douyin and other major platforms since May. It is already a default unspoken rule that the products of the shopping festival will "brightly descend and secretly ascend". Breaking this rule is very simple, you can use Python to write a small tool that regularly monitors commodity prices.

The first step is to grab the price of the product and store it in the SQLite database that comes with Python. Grab the price of the product regularly every day. Use the pyecharts module to draw a price line chart, so that the low price can be seen at a glance

grab price

Open the F12 control panel from the product details page, find the link containing p.3, and you can see the current product price in the preview panel next to it


sku_detail_url = 'http://item.jd.com/{}.html'
  sku_price_url = 'https://p.3.cn/prices/get?type=1&skuid=J_{}'
  r = requests.get(sku_detail_url.format(skuId)).content
  soup = BeautifulSoup(r, 'html.parser', from_encoding='utf-8')
  sku_name_div = soup.find('div', class_="sku-name")
  if not sku_name_div:
    print('The item you entered ID mistaken!')
    sku_name = sku_name_div.text.strip()
  r = requests.get(sku_price_url.format(skuId))
  price = json.loads(r.text)[0]['p']
  data = {
    'sku_id': skuId,
    'sku_name': sku_name,
    'price': price
  return data

Save the fetched price into the sqlite database, and use the Database function of PyCharm to create a sqlite database

Finally insert the data into the database

# new
def insert(data):
  conn = sqlite3.connect('price.db')
  c = conn.cursor()
  sql = 'INSERT INTO price (sku_id,sku_name,price) VALUES ("{}", "{}", "{}")'.format(data.get("sku_id"), data.get("sku_name"), data.get('price') )
# Inquire
def select(sku_id):
  conn = sqlite3.connect('price.db')
  c = conn.cursor()
  sql = 'select sku_id, sku_name, price, time from price where sku_id = "{}" order by time asc'.format(sku_id)
  cursor = c.execute(sql)
  datas = []
  for row in cursor:
    data = {
      'sku_id': row[0],
      'sku_name': row[1],
      'price': row[2],
      'time': row[3]
  return datas

Sample result

Use the lightweight schedule module to grab the price of Jingdong at 10:00 every morning

Install the schedule module

pip install schedule
def run_price_job(skuId):
  # Start a scheduled task in a way that does not occupy the main thread
  def run_continuously(interval=1):
    cease_continuous_run = threading.Event()
    class ScheduleThread(threading.Thread):
      def run(cls):
        while not cease_continuous_run.is_set():
    continuous_thread = ScheduleThread()
    return cease_continuous_run
  # Run at 10 o'clock every day, get_jd_price: task method, skuId: parameter of the task method
  schedule.every().day.at("10:00").do(get_jd_price, skuId=skuId)

View historical prices

Use the pytharts module to draw a line chart and visually view the price difference of each day

datas = select(skuId)
def line(datas):
  x_data = []
  y_data = []
  for data in datas:
    .add_yaxis(datas[0].get('sku_name'), y_data, is_connect_nones=True)
    .render("Commodity price history.html")


This article captures the price of Jingdong Mall, and friends can also build a script to capture the price of Taobao. Use Python to solve the little pain points in life, so that the wallet is no longer dry.

If you like our Python tutorial today, please contact us three times. If it is helpful to you, you can scan the following to get a full set of Python learning materials.

Tags: Python programming language

Posted by GreenMarch on Sun, 24 Jul 2022 22:24:43 +0530