亚马逊苹果手机数据爬取

亚马逊苹果手机相关信息爬取
import re
from  bs4 import BeautifulSoup
import time
import requests
amazon_url = 'https://www.amazon.cn/s/ref=sr_pg_2?rh=n%3A664978051%2Cn%3A665002051%2Ck%3Aiphone&page=1&bbn=665002051&keywords=iphone&ie=UTF8&qid=1523800289'
def get_page_num(url):
    '''此函数用来要爬取总获页面数'''
    user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
    Chrome/64.0.3282.186 Safari/537.36'
    headers = {'User-Agent':user_agent}#这是头信息,伪装爬虫
    res = requests.get(url,headers=headers)
    soup = BeautifulSoup(res.text,'lxml')
    page_num = soup.find('span',{'class':'pagnDisabled'}).get_text()
    return page_num
def save_data(title,price):
'''爬取的数据保存为文本格式'''
    with open('{}.txt'.format(time.strftime('%Y_%m_%d',time.localtime(time.time()))),'a') as f:
        #time.time()当前时间秒数
        #localtime()时间秒数转化为时间元祖
        #time.strftime(),时间元祖格式为字符串
        f.write(title + '\t' + price + '\n')
def down_data(page_num):
    for i in range(1,int(page_num)+1):
        url = 'https://www.amazon.cn/s/ref=sr_pg_2?rh=n%3A664978051%2Cn%3A665002051%2Ck%3Aiphone\
        &page={}&bbn=665002051&keywords=iphone&ie=UTF8&qid=1523800289'.format(i)
        user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)\
            Chrome/64.0.3282.186 Safari/537.36'
        headers = {'User-Agent':user_agent}#这是头信息,伪装爬虫
        res = requests.get(url,headers=headers)
        soup = BeautifulSoup(res.text,'lxml')
        titles = soup.find_all('h2',{'class':'a-size-base'})
        prices = soup.find_all('span',{'class':'a-size-base a-color-price s-price a-text-bold'})
        for i,j in zip(titles,prices):
            save_data(i.text,j.text)
if __name__ == '__main__':
    page_num = get_page_num(url)
    down_data(page_num)

你可能感兴趣的:(爬虫那点事)