1、 System analysis target page
2、html Label data analysis method
3、 Save massive data with one click
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 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/'
# 1. Send a request ( Visit website )
# # Print
# print(url)
# Send a request The way
response = requests.get(url=url, headers=headers)
# 2. get data Web source code
html_data = response.text
# 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)
# 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])
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 !!!