日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python爬取蘇寧易購--jsonpath方法

系統(tǒng) 2503 0

蘇寧的爬取和京東的爬取是一樣的,方法類似
這是爬取京東的例子:
https://blog.csdn.net/Dream____Fly/article/details/99698222
現(xiàn)在分析蘇寧的首頁,這個(gè)頁面還算比較
python爬取蘇寧易購--jsonpath方法_第1張圖片
這個(gè)很容易獲取,獲取之后在前面拼接https就行了
到這里就可以看代碼操作了:

            
              import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
}

def two_request(new_url):
    print(new_url)
    # two_response = requests.get(url=new_url,headers=headers,verify=False)
    # two_soup = BeautifulSoup(two_response.text,'lxml')
    #需要什么加什么
def first_resquest(first_response):
    soup = BeautifulSoup(first_response.text,'lxml')
    first_body = soup.select('.u-items-list > .f-rt-list > ul > li > a')
    for num in first_body:
        new_url = 'https:' + str(num['href'])
        #發(fā)起二次請求
        two_request(new_url)

def main():
    url = 'https://pindao.suning.com/city/caidian.html?safp=d488778a.homepage1.99345513004.6'
    #第一次請求,獲得請求
    first_response = requests.get(url=url,headers=headers,verify=False)
    first_resquest(first_response)

if __name__ == '__main__':
    main()

            
          

接下來獲取蘇寧易購的商品評論,這里需要抓取json包
python爬取蘇寧易購--jsonpath方法_第2張圖片
到這里應(yīng)該已經(jīng)完成了所有的需求

2.蘇寧易購直接獲取商品的評論

            
              import urllib.request
import json,jsonpath

url = 'https://review.suning.com/ajax/cluster_review_lists/general-30075272-000000000627657477-0000000000-total-2-default-10-----reviewList.htm?callback=reviewList'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
}

request = urllib.request.Request(url=url,headers=headers)
content = urllib.request.urlopen(request).read().decode('utf8')
# print('*'*100)
# print(content)
# print('*'*100)
content = content.strip('reviewList()')
# print('-'*100)
# print(content)
# print('-'*100)
obj = json.loads(content)
#找到所有的品論列表
comments = obj['commodityReviews']
fp = open('蘇寧評論.txt','w',encoding='utf8')
for comment in comments:
    #評論時(shí)間
    publishTime = comment['publishTime']
    #用戶
    nickname = comment['userInfo']['nickName']
    #評論內(nèi)容
    content = comment['content']
    #圖片地址
    is_have = comment['picVideoFlag']
    if is_have == True:
        image_src = jsonpath.jsonpath(comment,'$..imageInfo[*].url')
    else:
        image_src = "無"

    #保存
    item = {
        '評論時(shí)間':publishTime,
        '用戶':nickname,
        '評論內(nèi)容':content,
        '圖片地址':image_src,
    }
    string = str(item)
    fp.write(string + '\n')


            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦?。?!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 沅江市| 万荣县| 平乡县| 根河市| 资阳市| 綦江县| 前郭尔| 邻水| 陈巴尔虎旗| 始兴县| 资源县| 涡阳县| 富锦市| 久治县| 长葛市| 台南市| 济南市| 洛扎县| 凤城市| 东阳市| 体育| 吉林市| 汝南县| 连州市| 贡觉县| 东乡族自治县| 齐齐哈尔市| 三明市| 佳木斯市| 永和县| 宜兴市| 威宁| 安岳县| 竹山县| 故城县| 蒲城县| 抚州市| 长子县| 名山县| 霍州市| 章丘市|