安康有建网站的公司吗,湖南手机网站建设,妇科医院网站优化服务商,中国建设银行官网网址是多少最近帮一个朋友做一个抓取淘宝直通车数据的小项目#xff0c;感觉ython比较适合写爬虫程序#xff0c;决定使用Python来做程序。首先是登陆程序#xff0c;因为淘宝的登陆校验很复杂#xff0c;所以不能直接使用命令行的形式输入账号密码。查阅资料后#xff0c;发现可以使…最近帮一个朋友做一个抓取淘宝直通车数据的小项目感觉ython比较适合写爬虫程序决定使用Python来做程序。首先是登陆程序因为淘宝的登陆校验很复杂所以不能直接使用命令行的形式输入账号密码。查阅资料后发现可以使用Selenium的自动测试框架决定用这个框架实现登陆。首先下载一个纯净版的firefox浏览器放到主目录下然后用python打开浏览器def openbrowser_login():binaryFirefoxBinary(os.getcwd()/Firefox/Firefox.exe)profileFirefoxProfile()profile.set_preference(browser.cache.disk.enable,False)profile.set_preference(browser.cache.offline.enable,False)driverwebdriver.Firefox(firefox_binarybinary,firefox_profileprofile)driver.get(http://zhitongche.taobao.com/)while(True):if(len(driver.window_handles)1):print(检测到页面跳转)driver.switch_to.window(driver.window_handles[1]);time.sleep(3)driver.get(driver.current_url)time.sleep(5)break;else:time.sleep(2)cookie [item[name] item[value] for item in driver.get_cookies()]cookiestr;.join(item for item in cookie)try:driver.quit()except Exception as e:passreturn cookiestr实现的方式就是先去文件目录下找到firefox的启动文件然后使用浏览器打开淘宝直通车的登陆页程序每隔两秒检测一次页面如果发现新开了额外的标签就认为是登录成功这时把页面的cookie保存下来并返回。打开浏览器时同时设置了一些属性profile是浏览器属性设置文件这里将浏览器缓存功能关闭。下面是实现检查登陆的函数def check_login(cookiestr):print(开始登陆验证!)urlhttps://i.taobao.com/my_taobao.htmheaders {Host:i.taobao.com,User-Agent : Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0,Accept:text/html,application/xhtmlxml,application/xml;q0.9,*/*;q0.8,Accept-Language:zh-CN,zh;q0.8,en-US;q0.5,en;q0.3,# Accept-Encoding:gzip, deflate,Referer :https://www.taobao.com,Content-Type: application/x-www-form-urlencoded,Connection : Keep-Alive,Cookie : cookiestr,Cache-Control:max-age0,}requesturllib.request.Request(url,headersheaders)try:responseurllib.request.urlopen(request)# print(response.geturl())if(response.geturl()url):print(登陆验证通过!)return Trueexcept Exception as e:print(e)print(登陆验证失败!请重新登陆!)return False然后是检查淘宝直通车权限如果检查权限通过就将cookie文件保存下来方便下次使用def check_subway(cookiestr):print(开始淘宝直通车验证!)urlhttp://subway.simba.taobao.com/bpenv/getLoginUserInfo.htmheaders {Host:subway.simba.taobao.com,User-Agent : Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0,Accept:application/json, text/javascript, */*; q0.01,Accept-Language:zh-CN,zh;q0.8,Connection : Keep-Alive,Cookie : cookiestr,Origin:http://subway.simba.taobao.com,Cache-Control:max-age0,X-Requested-With:XMLHttpRequest}requesturllib.request.Request(url,headersheaders)data{_referer:/tools/insight/index}postdataurllib.parse.urlencode(data).encode(utf-8)try:responseurllib.request.urlopen(request,datapostdata)stringresponse.read().decode()parsejson.loads(string)if(parse[code]200):print(淘宝直通车验证通过!您当前以登陆)fpopen(cookie,wt)fp.write(cookiestr)fp.close()print(登陆cookie已经保存!)return parse[result][token]except Exception as e:print(e)print(淘宝直通车验证失败!请重新登陆!)return False在主函数中程序将优先加载cookie文件cookie失效或没有cookie文件时打开浏览器进行登陆#主函数if(os.path.exists(cookie)):print(检测到cookie文件将使用cookie登陆)fpopen(cookie,r)cookiestrfp.read()fp.close()else:cookiestropenbrowser_login()while(True):if(check_login(cookiestr)):tokencheck_subway(cookiestr)if(token!False):break;cookiestropenbrowser_login()