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

Python takes you to collect car data, which can be used as a reference when buying a car in the future~

編輯:Python

Catalog ( You can click on the place you want to see )

      • This code is provided by : Qingdeng Education - Teacher Siyue
  • Highlights of this time :
  • Introduction to the environment :
  • Code implementation :
  • Code
      • The import module
      • camouflage
      • Send a request
      • get data
      • Parsing data
      • Save the data
  • Tail language


This code is provided by : Qingdeng Education - Teacher Siyue


Highlights of this time :

1、 System analysis target page

2、html Label data analysis method

3、 Save massive data with one click

Introduction to the environment :

  • python 3.8
  • pycharm 2021 pro >>> Activation code
  • requests >>> pip install requests
  • parsel >>> pip install parsel

Code implementation :

  1. Send a request ( Visit website )
  2. get data Web source code
  3. Parsing data ( Web page construction , css Style selector )
  4. Save the data

Code

The import module

import requests # Send a request Third-party module ( be based on urllib Development module ) Additional installation pip install Module name
import parsel # Parsing data module Third-party module Additional installation pip install Module name
import csv # Built-in module You don't need to install

camouflage

# camouflage Request header request headers
headers = {

# Basic information of users , No login information
'Cookie': 'fvlid=1644577630437PyNsv7r4UeWZ; che_sessionid=6033453E-99CD-4D18-9EC6-26D8D81CB82E%7C%7C2022-02-11+19%3A07%3A13.352%7C%7C0; sessionid=1510cb04-9fdd-428f-82f7-d7b77ccae016; area=430103; sessionip=175.0.63.33; sessionvisit=98f1634e-6cbb-4f5e-97f9-6af88e5b8d33; sessionvisitInfo=1510cb04-9fdd-428f-82f7-d7b77ccae016|www.autohome.com.cn|100533; Hm_lvt_d381ec2f88158113b9b76f14c497ed48=1650521306; che_sessionvid=6223C48B-0DCD-4485-8DB7-B9A2D39AC7FF; carDownPrice=1; listuserarea=0; UsedCarBrowseHistory=0%3A43390985%2C0%3A43390239%2C0%3A43268434%2C0%3A43010424; userarea=0; ahpvno=23; Hm_lpvt_d381ec2f88158113b9b76f14c497ed48=1650523580; ahuuid=46A0D5A6-5C39-4735-A5CE-3F6DA42672C9; showNum=52; v_no=36; visit_info_ad=6033453E-99CD-4D18-9EC6-26D8D81CB82E||6223C48B-0DCD-4485-8DB7-B9A2D39AC7FF||-1||-1||36; che_ref=www.autohome.com.cn%7C0%7C100533%7C0%7C2022-04-21+14%3A46%3A20.424%7C2022-02-11+19%3A55%3A59.495; sessionuid=1510cb04-9fdd-428f-82f7-d7b77ccae016',
# Browser basic information
# Source code 、 answer 、 You can dress up if necessary :8321 Delete 57862
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
}
# z = 1, y = 2
url = 'https://www.che168.com/china/list/'

Send a request

# 1. Send a request ( Visit website )
# # Print
# print(url)
# Send a request The way
response = requests.get(url=url, headers=headers)

get data

# 2. get data Web source code
html_data = response.text

Parsing data

# 3. Parsing data ( Web page construction , css Style selector )
# Data analysis
# Website development >>> There are
# Artificial intelligence
# html( Store the data ) + css( style ) + js( Dynamic effects can be achieved )
# <div class="viewlist_ul">
# Vehicle information
# </div>
# <div>
# Owner information ...
# Source code 、 answer 、 You can dress up if necessary :8321 Delete 57862
# </div>
# <img />
selector = parsel.Selector(html_data)
# python  Data container list [1,3,4,5,6]
# I'm going to use an alias to get
lis = selector.css('.viewlist_ul li')
for li in lis:
card_name = li.css('.card-name::text').get()
cards_unit = li.css('.cards-unit::text').get()
price = li.css('.pirce em::text').get()
img_url = 'https:'+li.css('.img-box img::attr(src)').get()
if price == None or price == "":
price = li.css('.pirce::text').get()
print(card_name, cards_unit, price, img_url)

Save the data

 # 4. Save the data
with open(' Car home .csv', mode='a', encoding='utf-8', newline='') as f:
csv_writer = csv.writer(f)
csv_writer.writerow([card_name, cards_unit, price, img_url])

Tail language

Okay , My article ends here !

There are more suggestions or questions to comment on or send me a private letter ! Come on together and work hard (ง •_•)ง

If you like, just pay attention to the blogger , Or like the collection and comment on my article !!!


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