程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Use Python to obtain the data of an s-special game in real time. I will see who has money in his wallet

編輯:Python

Preface

Hello, everyone , I'm a panda

Steam You should be familiar with it ? If you don't know, let's have a look ~( For short “S”)

S By the American video game company Valve On 2003 year 9 month 12 The digital distribution platform launched on the th , It is considered to be one of the largest digital distribution platforms in the computer game industry ,S The platform is one of the largest comprehensive digital distribution platforms in the world . Players can buy 、 download 、 Discuss 、 Upload and share games and software .

And every week S The meeting opened a round of special offers , You can discount the game , And players will buy the game they want

It is said that every time there is a big discount , Countless players will buy games , It can make G Fat loss death


however , For a variety of reasons , I always miss the special price of some games I want to play !!!
therefore , I was thinking. , May I use Python collect S Data on all weekly special games

Code section

development environment

  • Python 3.8
  • Pycharm

First import the required modules

import random
import time
import requests
import parsel
import csv

Modules can pycharm Direct installation in , Input pip install XXX( Module name ) Just go

Request data

url = f'https://store. Input the website yourself , I didn't put the copyright here .com/contenthub/querypaginated/specials/TopSellers/render/?query=&start=1&count=15&cc=TW&l=schinese&v=4&tag='
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
response = requests.get(url=url, headers=headers)

Get the requested data

html_data = response.json()['results_html']
print(html_data)

In this way, the source code of the web page is obtained

Parsing data

selector = parsel.Selector(html_data)
lis = selector.css('a.tab_item')
for li in lis:
href = li.css('::attr(href)').get()
title = li.css('.tab_item_name::text').get()
tag_list = li.css('.tab_item_top_tags .top_tag::text').getall()
tag = ''.join(tag_list)
price = li.css('.discount_original_price::text').get()
price_1 = li.css('.tab_item_discount .discount_final_price::text').get()
discount = li.css('.tab_item_discount .discount_pct::text').get()
print(title, tag, price, price_1, discount, href)

Save the data

First save the data in the dictionary

dit = {
' game ': title,
' label ': tag,
' The original price ': price,
' The price is ': price_1,
' discount ': discount,
' Details page ': href,
}
csv_writer.writerow(dit)
At the end of the article, you can scan to get the source code

Finally save it to csv in

f = open(' game _1.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
' game ',
' label ',
' The original price ',
' The price is ',
' discount ',
' Details page ',
])
csv_writer.writeheader()

The final results

I'm a panda , See you in the next article (*◡‿◡)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved