摘要:自動(dòng)化爬取淘寶中的訂單這是淘寶會(huì)員登錄頁(yè)。但淘寶的反爬機(jī)制很難算出,很多都是通過(guò)的計(jì)算,所以不得不學(xué)習(xí)源碼,反到最后看的頭痛。。。
自動(dòng)化爬取淘寶中的訂單
這是 淘寶會(huì)員登錄頁(yè) 。因?yàn)橹白龅呐老x(chóng)都是通過(guò)框架或從登錄頁(yè)取得Cookie,再注入進(jìn)去實(shí)現(xiàn)登陸過(guò)程的。但淘寶的反爬機(jī)制很難算出Cookie,很多Cookie都是通過(guò)JS的計(jì)算,所以不得不學(xué)習(xí)源碼,反到最后看的頭痛。。。
第一次嘗試(1)登錄
通過(guò) Jsoup get登錄頁(yè)成功返回Cookie:
/** * 初始化淘寶登錄頁(yè) */ Response firstLoginInitResp = Jsoup.connect("https://login.taobao.com/member/login.jhtml?redirectURL=http%3A%2F%2Ftrade.taobao.com%2Ftrade%2Fitemlist%2Flist_export_order.htm") .header("Host", "login.taobao.com") .header("Connection", "keep-alive") .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") .header("Upgrade-Insecure-Requests", "1") .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36") .header("Accept-Encoding", "gzip, deflate, sdch") .header("Accept-Language", "zh-CN,zh;q=0.8") .execute(); MapfirstLoginInitCookies = firstLoginInitResp.cookies(); System.out.println("code: "+firstLoginInitResp.statusCode()+", msg: "+firstLoginInitResp.statusMessage()+", 第一次登陸淘寶返回的Cookie: "+firstLoginInitCookies.toString());
_tb_token_=e71873665bdae t=7770a28456dfcad8106b11406e3bc765 cookie2=17c4314a2a5b448f59aa038202b96019 v=0
返回成功后,JS動(dòng)態(tài)添加了倆個(gè)Cookie:
l= isg=
最后將Cookie重新注入,并傳送消息體到登錄頁(yè)(這是為了js再次動(dòng)態(tài)設(shè)置Cookie)
Response secondLoginInitResp = Jsoup.connect("https://login.taobao.com/member/login.jhtml?redirectURL=http%3A%2F%2Ftrade.taobao.com%2Ftrade%2Fitemlist%2Flist_export_order.htm%3Fpage_no%3D1") .header("Host", "login.taobao.com") .header("Connection", "keep-alive") .header("Content-Length", secondLoginInitData.length()+"") .header("Cache-Control", "max-age=0") .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") .header("Origin", "https://login.taobao.com") .header("Upgrade-Insecure-Requests", "1") .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36") .header("Content-Type", "application/x-www-form-urlencoded") .referrer("https://login.taobao.com/member/login.jhtml?redirectURL=http%3A%2F%2Ftrade.taobao.com%2Ftrade%2Fitemlist%2Flist_export_order.htm%3Fpage_no%3D1") .header("Accept-Encoding", "gzip, deflate") .header("Accept-Language", "zh-CN,zh;q=0.8") .cookies(firstLoginInitCookies) .data(secondLoginInitMap) .execute(); MapsecondLoginInitCookies = secondLoginInitResp.cookies(); System.out.println("code: "+secondLoginInitResp.statusCode()+", msg: "+secondLoginInitResp.statusMessage()+", 第二次登陸淘寶返回的Cookie: "+secondLoginInitCookies.toString());
結(jié)果返回的Cookie為空。此處省略過(guò)多廢話(huà)。。。只好再采用其他方式。
第二次嘗試這次將采用Selenium自動(dòng)化框架完成自動(dòng)登錄,再獲取Cookie注入到請(qǐng)求中,最后完成爬取。
因?yàn)樾枰脼g覽器來(lái)完成自動(dòng)化登錄,所以應(yīng)注意Firefox、Chrome、IE與Selenium對(duì)應(yīng)的版本(本人火狐版本24 下載地址、Selenium2.40 下載地址)。
import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import org.jsoup.Connection.Response; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import common.DateUtil; import common.FileUtil; import common.Log; /** * 淘寶爬蟲(chóng) * @author Alex * @date 2017年3月22日 */ public class TaobaoCrawler extends Log{ public String login(String username, String password){ logger.info("Start firefox browser succeed..."); try { WebDriver webDriver = new FirefoxDriver(); //創(chuàng)建火狐驅(qū)動(dòng)(谷歌IE需下載驅(qū)動(dòng)程序并添加瀏覽器插件,還有注意版本對(duì)應(yīng),比較麻煩,請(qǐng)百度版本對(duì)應(yīng)) webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); webDriver.get("https://login.taobao.com/member/login.jhtml?redirectURL=http://trade.taobao.com/trade/itemlist/list_export_order.htm?page_no=1"); WebElement passLoginEle= webDriver.findElement(By.xpath("http://a[@class="forget-pwd J_Quick2Static" and @target="_blank" and @href=""]")); //密碼登錄 logger.info("密碼登錄是否顯示可見(jiàn):"+passLoginEle.isDisplayed()); passLoginEle.click(); //顯示賬號(hào)密碼表單域(模仿點(diǎn)擊事件,將隱藏視圖變?yōu)榭梢?jiàn)) webDriver.findElement(By.id("TPL_username_1")).clear(); webDriver.findElement(By.id("TPL_username_1")).sendKeys(username); //輸入用戶(hù)名 webDriver.findElement(By.id("TPL_password_1")).clear(); webDriver.findElement(By.id("TPL_password_1")).sendKeys(password); //輸入密碼 webDriver.findElement(By.id("J_SubmitStatic")).click(); //點(diǎn)擊登錄按鈕 webDriver.switchTo().defaultContent(); try { while (true) { //不停的檢測(cè),一旦當(dāng)前頁(yè)面URL不是登錄頁(yè)面URL,就說(shuō)明瀏覽器已經(jīng)進(jìn)行了跳轉(zhuǎn) Thread.sleep(500L); if (!webDriver.getCurrentUrl().startsWith("https://login.taobao.com/member/login.jhtml")) { break; } } } catch (InterruptedException e) { e.printStackTrace(); } //獲取cookie,上面一跳出循環(huán)我認(rèn)為就登錄成功了,當(dāng)然上面的判斷不太嚴(yán)格,可以再進(jìn)行修改 StringBuffer cookieStr = new StringBuffer(); Setcookies = webDriver.manage().getCookies(); for (Cookie cookie : cookies) { cookieStr.append(cookie.getName() + "=" + cookie.getValue() + "; "); } logger.info("賬號(hào) "+username+" ,用戶(hù)登錄成功"); return cookieStr.toString(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); logger.info("賬號(hào) "+username+" ,用戶(hù)登錄失敗,可能被校驗(yàn)碼攔截"); logger.error(e.getMessage()); return null; } } public String getOrderUrl(String cookie){ try { Response orderResp = Jsoup.connect("https://trade.taobao.com/trade/itemlist/list_export_order.htm?page_no=1") .header("Host", "login.taobao.com") .header("Connection", "keep-alive") .header("Cache-Control", "max-age=0") .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*;q=0.8") .header("Origin", "https://login.taobao.com") .header("Upgrade-Insecure-Requests", "1") .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36") .header("Content-Type", "application/x-www-form-urlencoded") .header("Accept-Encoding", "gzip, deflate") .header("Accept-Language", "zh-CN,zh;q=0.8") .cookie("Cookie", cookie) .execute(); logger.info("請(qǐng)求訂單頁(yè)返回的code: "+orderResp.statusCode()+", msg: "+orderResp.statusMessage()); Document doc = orderResp.parse(); Element orderEle = doc.getElementsByAttributeValue("title", "下載訂單報(bào)表").get(0); //獲取第一個(gè) String orderUrl = orderEle.attr("href"); logger.info("訂單下載地址:"+orderUrl); return orderUrl; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); logger.error(e.getMessage()); return null; } } public static void main(String[] args){ TaobaoCrawler crawler = new TaobaoCrawler(); Map map = FileUtil.propToMap(); String cookie = crawler.login(map.get("username"), map.get("password")); String orderUrl = crawler.getOrderUrl(cookie); } }
普通驗(yàn)證碼是可以獲取的,但是通過(guò)以拖動(dòng)滑塊來(lái)驗(yàn)證用戶(hù)身份,這種情況就很難解決了。希望大家有空能試下,多提供些寶貴意見(jiàn)。。。
先這樣吧,不太會(huì)寫(xiě)文章,希望大家海涵。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/66873.html
摘要:一個(gè)網(wǎng)站使用的特征就是源代碼里包含了入口比如如果你在一個(gè)網(wǎng)站上看到了,那么采集這個(gè)網(wǎng)站數(shù)據(jù)的時(shí)候要格外小心。直接點(diǎn)擊下拉框中的選項(xiàng)不一定可行。未審核初審?fù)ㄟ^(guò)復(fù)審?fù)ㄟ^(guò)審核不通過(guò)專(zhuān)門(mén)提供了類(lèi)來(lái)處理下拉框。 JavaScript JavaScript 是網(wǎng)絡(luò)上最常用也是支持者最多的客戶(hù)端腳本語(yǔ)言。它可以收集 用戶(hù)的跟蹤數(shù)據(jù),不需要重載頁(yè)面直接提交表單,在頁(yè)面嵌入多媒體文件,甚至運(yùn)行網(wǎng)頁(yè)游戲。...
小編寫(xiě)這篇文章的主要目的,就是給大家介紹關(guān)于Python的一些總結(jié),比如使用Python爬蟲(chóng)Xpath定位數(shù)據(jù),那么,在定位數(shù)據(jù)的時(shí)候,有兩種方法,具體有什么方法介紹呢?下面就給大家詳細(xì)的解答下?! 》椒ㄒ唬褐苯佑益I,將文章路徑復(fù)制下來(lái)點(diǎn)擊Copy full Xpath 使用selenium+lxml中的etree進(jìn)行配合使用,使用etree解析html網(wǎng)頁(yè) importrequests ...
摘要:總的來(lái)說(shuō)有兩種反爬策略,要么驗(yàn)證身份,把蟲(chóng)子踩死在門(mén)口要么在網(wǎng)站植入各種反爬機(jī)制,讓爬蟲(chóng)知難而退。本節(jié)內(nèi)容就著這兩種反爬策略提出一些對(duì)策。內(nèi)嵌反爬很靈活,沒(méi)有什么固定的代碼格式,要花時(shí)間去分析出來(lái)。 ??之前提到過(guò),有些網(wǎng)站是防爬蟲(chóng)的。其實(shí)事實(shí)是,凡是有一定規(guī)模的網(wǎng)站,大公司的網(wǎng)站,或是盈利性質(zhì)比較強(qiáng)的網(wǎng)站,都是有高級(jí)的防爬措施的??偟膩?lái)說(shuō)有兩種反爬策略,要么驗(yàn)證身份,把蟲(chóng)子踩死在門(mén)口...
摘要:前端每周清單第期現(xiàn)狀分析與優(yōu)化策略單元測(cè)試爬蟲(chóng)作者王下邀月熊編輯徐川前端每周清單專(zhuān)注前端領(lǐng)域內(nèi)容,以對(duì)外文資料的搜集為主,幫助開(kāi)發(fā)者了解一周前端熱點(diǎn)分為新聞熱點(diǎn)開(kāi)發(fā)教程工程實(shí)踐深度閱讀開(kāi)源項(xiàng)目巔峰人生等欄目。 showImg(https://segmentfault.com/img/remote/1460000011008022); 前端每周清單第 29 期:Web 現(xiàn)狀分析與優(yōu)化策略...
摘要:基本環(huán)境安裝安裝下載注意要和版本對(duì)應(yīng)由于下載的是版本的,所以選擇了版本解壓此文件,并將文件移動(dòng)到目錄下測(cè)試是否可用,請(qǐng)執(zhí)行以下腳本,如返回內(nèi)容,則說(shuō)明安裝成功注意需要加上,禁止在沙箱中運(yùn)行補(bǔ)充安裝支持測(cè)試腳本 基本環(huán)境:centos7,python3.x 1.安裝selenium pip3 install selenium 2.安裝chrome-browser wget https:/...
閱讀 1523·2021-11-22 14:44
閱讀 2900·2021-11-16 11:44
閱讀 3271·2021-10-13 09:40
閱讀 2094·2021-10-08 10:04
閱讀 2429·2021-09-24 10:28
閱讀 2975·2021-09-06 15:02
閱讀 3030·2019-08-30 15:52
閱讀 2478·2019-08-30 13:20