python爬蟲爬取車標網所有車標Logo

1.依賴

requests、BeautifulSoup、lxml

2.代碼

爬取網址:http://www.chebiao.com.cn/chebiao/

import requests
from bs4 import BeautifulSoup

list = ['A', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'W', 'X', 'Y', 'Z']
#用於存車標網分類地址
newList = []
www = "http://www.chebiao.com.cn"
#根據網站分析可知,每種開頭的車標url分別爲http://www.chebiao.com.cn/字母/
for i in list:
    temp = www + "/chebiao/" + i.lower() + "/"
    newList.append(temp)

#用於存獲取到的圖片url地址
imgUrls = []
#用於存車標名字,順序和圖片順序一致
nameList = []

#爬取圖片地址
for x in newList:
    response = requests.get(x, timeout=3000)
    response.encoding = 'gbk'
    soup = BeautifulSoup(response.text, 'lxml')
    lis = soup.select(".chebiao ul li")
    for li in lis:
        imgUrls.append(www + str(li.select_one("img")['src']))
        nameList.append(li.select_one("span").text)

index = 0
#根據圖片url list中的地址下載圖片到本地
for url in imgUrls:
    request1 = requests.get(url)
    path = 'C:\\Users\\Lance\\Desktop\\1\\' + str(nameList[index][0:-2]+".jpg")
    with open(path, 'wb') as f:
        f.write(request1.content)
        f.flush()
        f.close()
    index = index + 1

3.效果圖 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章