蘇寧的爬取和京東的爬取是一樣的,方法類似
這是爬取京東的例子:
https://blog.csdn.net/Dream____Fly/article/details/99698222
現在分析蘇寧的首頁,這個頁面還算比較
這個很容易獲取,獲取之后在前面拼接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'])
#發起二次請求
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包
到這里應該已經完成了所有的需求
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:
#評論時間
publishTime = comment['publishTime']
#用戶
nickname = comment['userInfo']['nickName']
#評論內容
content = comment['content']
#圖片地址
is_have = comment['picVideoFlag']
if is_have == True:
image_src = jsonpath.jsonpath(comment,'$..imageInfo[*].url')
else:
image_src = "無"
#保存
item = {
'評論時間':publishTime,
'用戶':nickname,
'評論內容':content,
'圖片地址':image_src,
}
string = str(item)
fp.write(string + '\n')
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
