简单的爬虫代码(Amazon)

爬取亚马逊商品评论

import requests
import re
from bs4 import BeautifulSoup
from wca import create
import json
import openpyxl


headers = {
    'cookie': 'session-id=141-1894534-1178019; session-id-time=2082787201l; i18n-prefs=USD; ubid-main=135-4702725-2873831; lc-main=zh_CN; sp-cdn="L5Z9:CN"; session-token=eNLeoBT2mh3t9TXlsMz30p8eZLtdgMl4JGw3Ww0FtJUuey87Yhnd6MctLDawM81u0gSTSK1MRQ1SGFSaI1IX90G4GI9XQBCdyT5U6YWspKMI3T35uuykbgLp/L5eKfssk4KmEb/pbRi5ASQkT2878ksz/RPCfw320g01wlLoLt8ufDpM92frrZilc6++UusZn5GINn5pwoKgqp1jv3vkE8nfSA6fgx9x/mNb3Sw4ieA=; csm-hit=adb:adblk_no&t:1683439336293&tb:7A42W6J79HAD3WT8J0TD+sa-BXPK16W2AH53A6A0TC4S-A19JVG1Z46DH3DT28F7M|1683439336293',
    'referer': 'https://www.amazon.com/-/zh/dp/B08BHJ5XH9/ref=sr_1_7?crid=3PR2HOFK5T4AB&keywords=Mobile%2Bphone&qid=1680827474&refinements=p_89%3AApple&rnid=2528832011&s=wireless&sprefix=%E6%89%8B%E6%9C%BA%2Caps%2C445&sr=1-7&th=1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.68',
}
def main(url):
    response = requests.get(url=url,headers=headers)
    soup = BeautifulSoup(response.text,'lxml')
    comment_list = []
    comment_list = soup.find_all('div', attrs={'data-hook':'review'})


    for comment in comment_list:
        user = comment.find('span', attrs={'class':'a-profile-name'}).text
        content = comment.find('span', attrs={'data-hook': 'review-body'}).text
        score = comment.find('span', attrs={'class': 'a-icon-alt'}).text
        useful = comment.find('span', attrs={'data-hook': 'review-voting-widget'}).text
        status = comment.find('span', attrs={'data-hook':'avp-badge'}).text
        date = comment.find('span', attrs={'data-hook': 'review-date'}).text


        with open('a.txt','a',encoding='utf-8') as f0:
            data = {'user':user,'content':content,'score':score,'useful':useful,'status':status,'data':date}
            f0.write(json.dumps(data))
            f0.write('\n')
            f0.close()

        print('user',user)
        print('content',content)
        print('score',score)
        print('thought that useful-',useful)
        print('status',status)
        print('date',date)
        print('------------------------------------------------------------------------------')

        workbook = openpyxl.load_workbook('a.xlsx')
        worksheet = workbook.active
        data1 = {'user':user,'content':content,'score':score,'useful':useful,'status':status,'data':date}
        worksheet.append(list(data1.keys()))  
        worksheet.append(list(data1.values()))
        workbook.save('a.xlsx')

for i in range(30):
    print('page', i + 1)
    main('https://www.amazon.com/-/zh/product-reviews/B08BHJ5XH9/ref=cm_cr_getr_d_paging_btm_prev_1?ie=UTF8&reviewerType=all_reviews&pageNumber=' + str(i + 1))

f = open('c.txt', 'r', encoding='utf-8')
text = f.read()
create(text)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CYB-dengBer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值