ython讀取當(dāng)當(dāng)首頁的全部商品分類,首先要使用開發(fā)者工具找到全部商品分類所在的層級,
寫代碼來讀取該層級下的文本和超鏈接。
from __future__ import print_function
import requests
import sys
#reload(sys)
#sys.setdefaultencoding("utf-8")
import importlib
importlib.reload(sys)
from lxml import etree;
#打印全部商品分類
resp=requests.get('http://category.dangdang.com/?ref=www-0-C')
doc_main=etree.HTML(resp.content.decode("gbk"))
for x in doc_main.xpath("http://body/div/div/div/ul"):
print(*x.xpath("li/a/text()")+x.xpath("li/a/@href"))
1.商品分類是單獨的一項功能,所以商品分類的代碼需要新建一個文件/admin/category.php
2.創(chuàng)建商品分類表
3.插入數(shù)據(jù)
4.著手category.php。/admin/category.php
5.引入模板文件/admin/templates
6.加載模板文件顯示/admin/category.php
7.修改首頁中左側(cè)的菜單欄中的商品分類的url。/admin/templates/menu.html
8.驗證效果
凡是跟商品分類相關(guān)的操作都去請求category.php
1.創(chuàng)建一個Category類,專門用來操作sh_category表/includes/Category.class.php
2.通過Category類來獲取所有的商品分類/admin/category.php
3.實現(xiàn)getAllCategories方法/includes/Category.class.php
4.在模板中遍歷數(shù)據(jù)并顯示/admin/templates/category_list.html
現(xiàn)在數(shù)據(jù)已經(jīng)存在,但是商品分類并沒有進行真正分類,需要進行分類操作。商品分類沒有嚴格的確定層級的分類,可以無限擴展子分類所以被稱之為無限級分類。
5.無限級分類,調(diào)用無限級分類,對已經(jīng)得到的所有商品分類進行處理/admin/category.php
6.實現(xiàn)noLimitCategory方法/includes/Category.class.php
7.顯示的時候,按照level層級進行縮進/admin/templates/category_list.html
str_repeat():重復(fù)輸出某個字符串
無限級分類原理
1.獲取添加商品分類的模板文件/admin/templates/category_add.html
2.添加動作,能夠加載category_add.html。/admin/catetory.php
3.修改連接能夠進入到category.php?act=add。/admin/templates/category_list.html
上面實現(xiàn)進入到添加商品分類的界面,但是商品分類的新增需要指定父級分類,而現(xiàn)在沒有。
4.在加載模板之前,需要獲取所有的商品分類。/admin/category.php?act=add
5.在對應(yīng)模板里顯示所獲得的所有無限級分類。/admin/templates/category_add.html
增加回到商品列表的鏈接
私信小編01即可獲取大量Python學(xué)習(xí)資料
首先,我們打開首頁,輸入關(guān)鍵詞:女裝。↓↓↓
點擊找一下,后會跳轉(zhuǎn)到商品頁面,如下圖所示↓↓↓
這個時候我們就可以看到女裝商品分類,和一些推薦商品,
接下來我們不要急著爬這些商品數(shù)據(jù),我們要找的是這些商品的分類目錄地址。
谷歌瀏覽器右擊檢查頁面,仔細觀察會發(fā)現(xiàn),每個分類的商品都有對應(yīng)的地址,例如:連衣裙,對應(yīng)的地址如下
我們進入連衣裙的href標簽里面的地址,你會發(fā)現(xiàn)頁面的標題已經(jīng)從“女裝”變成“女裝-連衣裙”了,因為我們在這個頁面看到的商品是經(jīng)過淘寶分類后的,這一頁內(nèi)容只包含“女裝-連衣裙”。
通過抓包 我們發(fā)現(xiàn),發(fā)現(xiàn)這一頁的真實的數(shù)據(jù)來源地址是:
https://s.1688.com/selloffer/rpc_async_render.jsonp?cps=1&n=y&filtOfferTags=279874&filt=y&keywords=%C5%AE%D7%B0&&categoryId=0&n=y&uniqfield=pic_tag_id&templateConfigName=marketOfferresult&pageSize=60&asyncCount=60&async=true&enableAsync=true&rpcflag=new&_pageName_=market&callback=jQuery172015741463935213496_1555383468519&beginPage=1
聯(lián)系上下文,仔細觀察會發(fā)現(xiàn),這是一個可以拼接的url,大致拼接方式如下:???
url='https://s.1688.com/selloffer/rpc_async_render.jsonp?cps=1&n=y&filtOfferTags=279874&filt=y&keywords='+keywords+'&categoryId='+categoryId+'&n=y&uniqfield=pic_tag_id&templateConfigName=marketOfferresult&pageSize=60&asyncCount=60&async=true&enableAsync=true&rpcflag=new&_pageName_=market&callback=jQuery172015741463935213496_1555383468519&beginPage='+str(i)
其中keywords不難看出是關(guān)鍵詞,而且是進行url編碼后的,而 i 這個明顯是頁碼數(shù)字,categoryId英語好的一眼就知道是“類別ID”
這些參數(shù)是從哪來的呢?
回到前面,我們進入“女裝-連衣裙”的頁面,并查看源碼,搜索這些關(guān)鍵詞,
找到了:
接下來的事 就簡單了,通過填參數(shù)拼接url,我們隨意可以從女裝-連衣裙分類下,獲取幾十頁數(shù)據(jù)信息,或者從女裝-日韓女裝分類下獲取數(shù)據(jù)信息。然后通過正則匹配到商品offerid。???
這些offerid代表的就是商品id,例如取出其中一個offerid:556983465623。那么這個商品的完整地址就是:
https://detail.1688.com/offer/556983465623.html
商品的名稱、價格、銷量、大小參數(shù)都可以從這個地址獲取到。
下一篇我會教大家如何根據(jù)offerid抓取商品詳情。
本篇完整代碼如下:
???
# encoding: utf-8
"""
本腳本 用于根據(jù)關(guān)鍵詞“女裝”爬取1688全部分類商品的offerid
"""
import requests
import re
import random
from lxml import html
import time
"""獲取頁面內(nèi)容"""
def get_html(url):
html=''
for x in range(5):
try:
resp=requests.get(url)
html=resp.text
if len(html) < 1000:
continue
else:
return html
except Exception as e:
print('url {0}, throw exception: {1}'.format(url, e))
html=''
return html
"""從女裝首頁獲取全部的分類地址"""
def category_spider():
# 女裝:%C5%AE%D7%B0
url='https://s.1688.com/selloffer/offer_search.htm?keywords=%C5%AE%D7%B0&button_click=top&earseDirect=false&n=y&netType=1%2C11'
htmlstr=get_html(url)
section=html.fromstring(htmlstr)
links=section.xpath("//div[@class='s-widget-flatcat sm-widget-row sm-sn-items-control sm-sn-items-count-d fd-clr']/div[@class='sm-widget-items fd-clr']/ul//a/@href")
return links
"""從數(shù)據(jù)源中正則匹配商品的offerid"""
def spider(url):
pid_list=list()
htmlstr=get_html(url)
goods_pid=re.findall(r'offerid=.*?(\d+)', htmlstr)
for pid in goods_pid:
pid_list.append(pid)
return pid_list
def main():
# 獲取女裝商品下的所有分類目錄地址:連衣裙、女式T恤、短袖T恤、外貿(mào)裙、日韓女裝等等
links=category_spider()
# 遍歷所有分類
for link in links:
sound=get_html(link)
# 類別ID
categoryId=re.findall(r'"categoryId":"(\d+)"', sound)[0]
# 關(guān)鍵詞
keywords=re.findall(r'"keywordsGbk":"(.*?)"', sound)[0]
# 每個類別商品,取10頁數(shù)據(jù)
for i in range(1, 10):
url='https://s.1688.com/selloffer/rpc_async_render.jsonp?cps=1&n=y&filtOfferTags=279874&filt=y&keywords='+keywords+'&categoryId='+categoryId+'&n=y&uniqfield=pic_tag_id&templateConfigName=marketOfferresult&pageSize=60&asyncCount=60&async=true&enableAsync=true&rpcflag=new&_pageName_=market&callback=jQuery172015741463935213496_1555383468519&beginPage='+str(i)
pid_list=spider(url)
print(pid_list)
time.sleep(random.randint(1, 3))
if __name__=='__main__':
main()
代碼輸出結(jié)果展示:
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。