Preface
Code section
development environment
First import the required modules
Request data
Get the requested data
Parsing data
Save the data
PrefaceSteam 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 ,Steam 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 steam 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 steam Data on all weekly special games
Code section development environmentPython 3.8
Pycharm
First import the required modulesimport randomimport timeimport requestsimport parselimport csv
Modules can pycharm Direct installation in , Input pip install XXX( Module name ) Just go
Request dataurl = f'https://store.steampowered.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 dataselector = 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)
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
This is about using Python Get... In real time steam This is the end of the article on preferential game data , More about Python obtain steam Please search the previous articles of SDN or continue to browse the related articles below. I hope you can support SDN more in the future !