本文基于python3.4的selenium库打开浏览器,并将浏览器中的登陆cookie保存到本地,那么下次登陆就可以直接利用cookie了:

 # !/usr/bin/python3.4
# -*- coding: utf-8 -*- from selenium import webdriver
import time
import requests
from bs4 import BeautifulSoup
import os
import re
import random
import xlsxwriter # 找出文件夹下所有xml后缀的文件,可选择递归
def listfiles(rootdir, prefix='.xml', iscur=False):
file = []
for parent, dirnames, filenames in os.walk(rootdir):
if parent == rootdir:
for filename in filenames:
if filename.endswith(prefix):
file.append(filename)
if not iscur:
return file
else:
if iscur:
for filename in filenames:
if filename.endswith(prefix):
file.append(filename)
else:
pass
return file # 抓取dp的正则表达式
def getdp(string):
reg = r'(http.+?/dp/)(.+)'
all = re.compile(reg)
alllist = re.findall(all, string)
return alllist[0][1] # 抓取filter的正则表达式
# https://sellercentral.amazon.com/productsearch?filter=grocery&q=fish
def getfilter(string):
reg = r'(https.+?filter=)(.+?)(&)'
all = re.compile(reg)
alllist = re.findall(all, string)
return alllist[0][1] # 抓取最大页数的正则
def getpagenum(string):
reg = r'(.+?\()(\d+)(\))'
all = re.compile(reg)
alllist = re.findall(all, string)
return alllist[0][1] # 创建文件夹
def createjia(path):
try:
os.makedirs(path)
except:
pass def timetochina(longtime, formats='{}天{}小时{}分钟{}秒'):
day = 0
hour = 0
minutue = 0
second = 0
try:
if longtime > 60:
second = longtime % 60
minutue = longtime // 60
else:
second = longtime
if minutue > 60:
hour = minutue // 60
minutue = minutue % 60
if hour > 24:
day = hour // 24
hour = hour % 24
return formats.format(day, hour, minutue, second)
except:
raise Exception('时间非法') # 打开浏览器抓取cookie
def openbrowser(url):
# 打开谷歌浏览器
# Firefox() Chrome()
browser = webdriver.Chrome()
# browser = webdriver.Chrome(executable_path='C:/Python34/chromedriver.exe')
# 输入网址
browser.get(url)
# 打开浏览器时间
# print("等待10秒打开浏览器...")
# time.sleep(10) # 找到id="ap_email"的对话框
# 清空输入框
browser.find_element_by_id("ap_email").clear()
browser.find_element_by_id("ap_password").clear() # 输入账号密码
inputemail = input("请输入账号:")
inputpassword = input("请输入密码:")
browser.find_element_by_id("ap_email").send_keys(inputemail)
browser.find_element_by_id("ap_password").send_keys(inputpassword) # 点击登陆sign in
# id="signInSubmit"
browser.find_element_by_id("signInSubmit").click() # 等待登陆10秒
# print('等待登陆10秒...')
# time.sleep(10)
print("等待网址加载完毕...") select = input("请观察浏览器网站是否已经登陆(y/n):")
while 1:
if select == "y" or select == "Y":
print("登陆成功!")
# 获取cookie
cookie = [item["name"] + ":" + item["value"] for item in browser.get_cookies()]
cookiestr = ';'.join(item for item in cookie)
print("正在复制网页cookie...") # 写入本地txt
if "jp" in url:
path = "../data/Japcookie.txt"
else:
path = "../data/Amecookie.txt" filecookie = open(path, "w")
filecookie.write(cookiestr)
filecookie.close() time.sleep(1)
print("准备关闭浏览器...")
browser.quit()
# print(cookiestr)
break elif select == "n" or select == "N":
selectno = input("账号密码错误请按0,验证码出现请按1...")
# 账号密码错误则重新输入
if selectno == "": # 找到id="ap_email"的对话框
# 清空输入框
browser.find_element_by_id("ap_email").clear()
browser.find_element_by_id("ap_password").clear() # 输入账号密码
inputemail = input("请输入账号:")
inputpassword = input("请输入密码:")
browser.find_element_by_id("ap_email").send_keys(inputemail)
browser.find_element_by_id("ap_password").send_keys(inputpassword)
# 点击登陆sign in
# id="signInSubmit"
browser.find_element_by_id("signInSubmit").click() elif selectno == "":
# 验证码的id为id="ap_captcha_guess"的对话框
input("请在浏览器中输入验证码并登陆...")
select = input("请观察浏览器网站是否已经登陆(y/n):") else:
print("请输入“y”或者“n”!")
select = input("请观察浏览器网站是否已经登陆(y/n):") return cookiestr def gethtml(url):
# 读取cookie
# 写入字典
mycookie = {}
if "jp" in url:
path = "../data/Japcookie.txt"
else:
path = "../data/Amecookie.txt" try:
filecookie = open(path, "r")
cookies = filecookie.read().split(";")
for items in cookies:
item = items.split(":")
mycookie[item[0]] = item[1]
# print(mycookie)
filecookie.close()
except:
print("cookie为空...") if "jp" in url:
referer = "https://sellercentral.amazon.co.jp/"
host = "www.amazon.co.jp"
else:
referer = "https://sellercentral.amazon.com/"
host = "www.amazon.com" # 制作头部
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0',
'Referer': referer,
'Host': host,
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br'
} htmlget = requests.get(url=url, headers=header, cookies=mycookie, timeout=60)
htmlcontent = htmlget.content.decode("UTF-8", "ignore") return htmlcontent def getinfo(html, Loginurl):
# BeautifulSoup解析需要的东西
soups = BeautifulSoup(html, "html.parser")
# 筛选出商品的div
sellyours = soups.find_all("div", attrs={"class": "a-box product"})
information = []
for item in sellyours:
# 一个一个商品筛选
# 第一次筛选,得到有“出售您的”的商品列表
temp = item.find("a", attrs={"class", "a-button-text"}) if temp != None:
if "sellYoursClick" in temp["data-csm"]:
# 第二次筛选得到“无数字、无新品”字样的商品列表
temp = item.find("span", attrs={"class", "offerCountDetails"})
if temp == None:
temp = item.find("div", attrs={"class", "a-fixed-right-grid-col description a-col-left"}) # 得到详情页网址
hrefurl = temp.find('a').get('href')
# 得到所有当前class下的文本信息
# 包括title、UPC、EAN、Rank
try:
spans = temp.get_text()
except:
spans = "Nothing"
# 将得到的文本信息写入数组里面
temparr = spans.strip().split("\n")
# 正则得到Asin
asin = getdp(hrefurl)
temparr.append(asin)
temparr.append(hrefurl) # 这里记录一份副本到txt中,防止程序中断什么都没保存
txtcontent = ' '.join(temparr)
filename = time.strftime('%Y%m%d', time.localtime())
path = "../xls/" + filename
createjia(path)
file = open(path + "/" + filename + ".txt", "a")
file.write("\n" + txtcontent)
file.close() # 这里是解析详情页,如果详情页有price,就抓取review下来
# 并且将抓取的东西储存到数组,并写入excel中
# 解析详情页
htmldetail = gethtml(hrefurl) if 'id="words"' in htmldetail or 'ap_email' in htmldetail or "Amazon.com Page Not Found" in htmldetail:
print("抓取得太快!需要重新登陆...")
openbrowser(Loginurl)
htmldetail = gethtml(hrefurl) # BeautifulSoup解析需要的东西
soups = BeautifulSoup(htmldetail, "html.parser")
# 筛选出商品的centerCol列表
centerCols = soups.findAll('div', attrs={'id': "centerCol"})
if centerCols:
for item in centerCols:
temp = item.find("td", attrs={"id": "priceblock_ourprice_lbl"})
if temp == None:
# 得到评分等级
star = item.find("a", attrs={"id": "reviewStarsLinkedCustomerReviews"}).get_text()
# 得到评分人数
reviews = item.find("span", attrs={"id": "acrCustomerReviewText"}).get_text()
# 将抓取的东西写入数组
if star:
temparr.append(star.strip().replace(" out of 5 stars", ""))
else:
temparr.append("")
if reviews:
temparr.append(reviews.strip().replace(" customer reviews", ""))
else:
temparr.append("") information.append(temparr)
print(information)
else:
temparr.append("")
temparr.append("")
information.append(temparr)
print(information)
return information def begin():
taoyanbai = '''
-----------------------------------------
| 欢迎使用后台爬虫系统 |
| 时间:2016年10月21日 |
| 出品:技术部 |
-----------------------------------------
'''
print(taoyanbai) if __name__ == "__main__": a = time.clock() while 1:
try:
LoginWhere = int(input("抓取美国请按0,日本请按1:"))
if LoginWhere == 0:
Loginurl = "https://sellercentral.amazon.com/"
break
elif LoginWhere == 1:
Loginurl = "https://sellercentral.amazon.co.jp/"
break
except:
print("请正确输入0或1!!")
LoginWhere = int(input("抓取美国请按0,日本请按1:")) keywords = input("请输入查找的关键词:")
keyword = keywords.replace(" ", "+") print("正在检查登陆状态...") if "jp" in Loginurl:
seekurl = "https://sellercentral.amazon.co.jp/productsearch?q=" + str(keyword)
else:
seekurl = "https://sellercentral.amazon.com/productsearch?q=" + str(keyword) try:
htmlpage = gethtml(seekurl)
except Exception as err:
input("网络似乎有点问题...")
print(err)
exit() while 1:
if 'ap_email' in htmlpage or "Amazon.com Page Not Found" in htmlpage or "<title>404" in htmlpage:
print("cookie已经过期,需要重新登陆...")
print("等待网页打开...")
openbrowser(Loginurl)
htmlpage = gethtml(seekurl)
else:
print("直接使用cookie登陆...")
break # BeautifulSoup解析需要的东西
soups = BeautifulSoup(htmlpage, "html.parser")
# 筛选出类别及其网址
categorys = soups.findAll('ul', attrs={'class': "a-nostyle a-vertical"})
categoryurl = []
categoryname = ""
pagenum = []
filtername = [] for item in categorys:
for temp in item.find_all("a"):
hrefurl = temp.get('href')
categoryurl.append(hrefurl) for temp in item.find_all("span", attrs={"class", "a-color-tertiary"}):
spantext = temp.get_text()
pagenum.append(getpagenum(spantext))
for i in range(0, len(categoryurl)):
name = getfilter(categoryurl[i])
filtername.append(name)
categoryname = categoryname + "抓取(" + str(name) + ")请按" + str(i) + "," # 选择抓取的类型
try:
print(categoryname)
selectcategory = int(input("请选择你要抓取类型的数字号码:"))
except:
print("请正确输入前面的数字!!!")
print(categoryname)
selectcategory = int(input("请选择你要抓取类型的数字编码:")) filter = filtername[selectcategory]
mustpage = int(pagenum[selectcategory]) // 10 try:
print("温馨提醒:(1)后台仅仅展现1000页...(2)你要抓取的类型大约有" + str(mustpage) + "页...")
page = int(input("请问你要抓取多少页?(默认15页):"))
if page > 1000:
print("后台最多只能看到1000页!!!")
page = int(input("后台仅仅展现1000页!!!你要抓取的类型大约有" + str(mustpage) + "页!!!请问你要抓取多少页?(默认15页):"))
except:
page = 15 # 储存抓取到的东西
information = []
temparr = [] for i in range(0, page):
try:
if "jp" in Loginurl:
# https://sellercentral.amazon.co.jp/productsearch?filter=sporting&q=空気入れ&page=2
openurl = "https://sellercentral.amazon.co.jp/productsearch?filter=" + str(filter) + "&q=" + str(
keyword) + "&page=" + str(i + 1)
else:
# https://sellercentral.amazon.com/productsearch?filter=pets&q=dog
openurl = "https://sellercentral.amazon.com/productsearch?filter=" + str(filter) + "&q=" + str(
keyword) + "&page=" + str(i + 1) print("开始抓取:" + str(openurl))
openhtml = gethtml(openurl) # BeautifulSoup解析需要的东西
soups = BeautifulSoup(openhtml, "html.parser")
# 筛选出商品的div
sellyours = soups.findAll('div', attrs={'class': "product"}) if 'ap_email' in openhtml or "Amazon.com Page Not Found" in openhtml:
print("抓取得太快!需要重新登陆...")
openbrowser(Loginurl)
openhtml = gethtml(openurl) elif sellyours == None:
print("已经翻到最后一页了...")
break
temparr = getinfo(openhtml, Loginurl)
except Exception as err:
print(err)
print("访问抓取过程中出现小错误...")
print("暂停20秒记录bug并尝试自我修复...")
time.sleep(20) if temparr:
information.append(temparr[0])
loadtime = random.randint(5, 10)
print("防止反爬虫设定暂停" + str(loadtime) + "秒...")
time.sleep(loadtime) print("抓到的列表如下:")
print(information) # 这里写入excel
# 创建文件夹
filename = time.strftime('%Y%m%d', time.localtime())
path = "../xls/" + filename
createjia(path) # 写入excel
timename = time.strftime('%Y%H%M%S', time.localtime())
with xlsxwriter.Workbook(path + "/" + timename + '.xlsx') as workbook:
# workbook = xlsxwriter.Workbook(path + "/" + timename + '.xlsx')
worksheet = workbook.add_worksheet() first = ['title', 'UPC', 'EAN', 'Rank', 'Nothing', 'ASIN', 'DetailUrl', 'Star', 'Reviews']
# 写入第一行
for i in range(0, len(first)):
worksheet.write(0, i, first[i])
# 写入后面几行
for m in range(0, len(information)):
for n in range(0, len(information[m])):
insert = str(information[m][n]).replace("UPC: ", "").replace("EAN: ", "").replace("Sales Rank:",
"").replace(
"customer reviews", "").replace("out of 5 stars", "")
worksheet.write(m + 1, n, insert)
workbook.close() b = time.clock()
print('运行时间:' + timetochina(b - a))
input('请关闭窗口') ##防止运行完毕后窗口直接关闭而看不到运行时间

由于selenium库支持低版本的浏览器,例如本文的谷歌浏览器需要下载插件,并将插件放到目录C:\Python34即可:

插件为chromedriver.exe,自己搜索,网上很多哒

Amazon后台模拟登陆的更多相关文章

  1. Amazon后台登陆以及跟卖

    亚马逊模拟登陆,这里使用的是selenium来登陆,并判断是否登陆成功,以及是否有验证码,并破解验证码登陆. 跟卖主要解决的难题是selenium的新窗口弹出问题,在 # 点击“出售您的” brows ...

  2. php 的curl 模拟登陆

    做一个类似这样的web 应用. 1,解决掉验证码 其实这是正方的一个小bug,当我们进入登陆界面时,浏览器会去请求服务器,服务器会生成一个验证码图片.如果我们不去请求这个图片,那么正方后台也不会生成相 ...

  3. python模拟登陆之下载

    好长时间没有更新博客了,哈哈. 今天公司给了这么一个需求,现在我们需要去淘宝获取上一天的订单号,然后再根据订单号去另一个接口去获取订单详情,然后再给我展示到web! 中间涉及到的技术点有: 模拟登陆 ...

  4. 以正方教务系统为例,用php模拟登陆抓取课表、空教室

    课程格子和超级课程表这两个应用,想必大学生都很熟悉,使用自己的学号和教务系统的密码,就可以将自己的课表导入,随时随地都可以在手机上查看. 其实稍微了解一点php的话,我们也可以做一个类似这样的web ...

  5. HttpClient+Jsoup模拟登陆贺州学院教务系统,获取学生个人信息

    前言 注:可能学校的教务系统已经做了升级,当前的程序不知道还能不能成功获取信息,加上已经毕业,我的账户已经被注销,试不了,在这里做下思路跟过程的记录. 在我的毕业设计中”基于SSM框架贺州学院校园二手 ...

  6. 使用ApiPost测试接口时需要先登录怎么办?利用Cookie模拟登陆!

    ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . 下 ...

  7. 第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别

    第三百四十三节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别 第一步.首先下载,大神者也的倒立文字验证码识别程序 下载地址:https://gith ...

  8. .net+jquery+ashx实现客户端模拟登陆扩展

    客户端实现:login namespace LoginApp { partial class Form1 { /// <summary> /// 必需的设计器变量. /// </su ...

  9. Python爬虫学习笔记之模拟登陆并爬去GitHub

    (1)环境准备: 请确保已经安装了requests和lxml库 (2)分析登陆过程:     首先要分析登陆的过程,需要探究后台的登陆请求是怎样发送的,登陆之后又有怎样的处理过程.      如果已经 ...

随机推荐

  1. 通讯录CoreData数据库实现版

    一,创建新项目,选择空模板,名字MultyViewAddressBookCoreDataVersion,选择下面的use Core Data创建项目 首先在datamodel中创建一个实体,创建属性

  2. 第一个Shader的更新,增加爆光度, 属性改为数值型(更直观,精确)

    Shader "Castle/ColorMix" { Properties { // 基本贴图 _MainTex ("Texture Image", 2D) = ...

  3. magento和discuz(ucenter)整合集成开发思路

    discuz自带ucenter,主要就是用于和其他程序的通信.我们可以下载discuz的ucenter开发手册进行magento和discuz的集成.里面有一些ucenter的一些接口函数和参数说明, ...

  4. Magento创建configurable产品的要点

    接着上一篇用API创建可配置的产品Configurable Product说事.Magento的产品类型可分为Simple Product.Group Product.Configurable Pro ...

  5. 触摸事件UITouch的用法

    触摸屏幕是iOS设备接受用户输入的主要方式,包括单击.双击.拨动以及多点触摸等,这些操作都会产生触摸事件. 在Cocoa中,代表触摸对象的类是UITouch.当用户触摸屏幕后,就会产生相应的事件,所有 ...

  6. 《深入浅出Node.js》第2章 模块机制

    @by Ruth92(转载请注明出处) 第2章 模块机制 JavaScript 先天缺乏的功能:模块. 一.CommonJS 规范: JavaScript 规范的缺陷:1)没有模块系统:2)标准库较少 ...

  7. Open vSwitch FAQ (一)

    Basic Configuration Q: How do I configure a port as an access port? A: Add "tag=VLAN" to y ...

  8. 关于 System.IO.FileAttributes 的 Reparse Points

    关于Reparse Points找到下面的解释,要是能有更进一步的解释说明就更好了 Reparse Points其实是一个用户自定义的数据集合,它可以包含在一个文件或目录中.这种格式的数据能够被特定的 ...

  9. SQLITE LIMIT

    sqlite> create table t_user(userid int,username varchar(20)); sqlite> insert into t_user value ...

  10. ajax 城市区域选择三级联动

    <body onLoad="sheng()"><div class="xqbody">    <form action=" ...