Python查詢阿里巴巴關(guān)鍵字排名的方法
本文實例講述了Python查詢阿里巴巴關(guān)鍵字排名的方法。分享給大家供大家參考。具體如下:
這里使用python庫urllib及pyquery基本東西的應(yīng)用,實現(xiàn)阿里巴巴關(guān)鍵詞排名的查詢,其中涉及到urllib代理的設(shè)置,pyquery對html文檔的解析
1. urllib 基礎(chǔ)模塊的應(yīng)用,通過該類獲取到url中的html文檔信息,內(nèi)部可以重寫代理的獲取方法
class ProxyScrapy(object): def __init__(self): self.proxy_robot = ProxyRobot() self.current_proxy = None self.cookie = cookielib.CookieJar() def __builder_proxy_cookie_opener(self): cookie_handler = urllib2.HTTPCookieProcessor(self.cookie) handlers = [cookie_handler] if PROXY_ENABLE: self.current_proxy = ip_port = self.proxy_robot.get_random_proxy() proxy_handler = urllib2.ProxyHandler({'http': ip_port[7:]}) handlers.append(proxy_handler) opener = urllib2.build_opener(*handlers) urllib2.install_opener(opener) return opener def get_html_body(self,url): opener = self.__builder_proxy_cookie_opener() request=urllib2.Request(url) #request.add_header("Accept-Encoding", "gzip,deflate,sdch") #request.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") #request.add_header("Cache-Control", "no-cache") #request.add_header("Connection", "keep-alive") try: response = opener.open(request,timeout=2) http_code = response.getcode() if http_code == 200: if PROXY_ENABLE: self.proxy_robot.handle_success_proxy(self.current_proxy) html = response.read() return html else: if PROXY_ENABLE: self.proxy_robot.handle_double_proxy(self.current_proxy) return self.get_html_body(url) except Exception as inst: print inst,self.current_proxy self.proxy_robot.handle_double_proxy(self.current_proxy) return self.get_html_body(url)
2. 根據(jù)輸入的公司名及關(guān)鍵詞列表,返回每個關(guān)鍵詞的排名
def search_keywords_rank(keyword_company_name, keywords): def get_context(url): start=clock() html=curl.get_html_body(url) finish=clock() print url,(finish-start) d = pq(html) items = d("#J-items-content .ls-item") items_c = len(items) print items_c if items_c < 38: return get_context(url) return items, items_c result = OrderedDict() for keyword in keywords: for page_index in range(1,9): u = url % (re.sub('\s+', '_', keyword.strip()), page_index) items, items_c = get_context(u) b = False for item_index in range(0, items_c): e=items.eq(item_index).find('.title a') p_title = e.text() p_url = e.attr('href') e=items.eq(item_index).find('.cright h3 .dot-product') company_name = e.text() company_url = e.attr('href') if keyword_company_name in company_url: total_index = (page_index-1)*38 +item_index+1+(0 if page_index==1 else 5) print 'page %s, index %s, total index %s' % (page_index, item_index+1, total_index) b = True if keyword not in result: result[keyword] = (p_title, p_url, page_index, item_index+1, total_index, u) break if b: break return result
希望本文所述對大家的Python程序設(shè)計有所幫助。
- 在阿里云服務(wù)器上配置CentOS+Nginx+Python+Flask環(huán)境
- python模擬登陸阿里媽媽生成商品推廣鏈接
- python使用urllib模塊和pyquery實現(xiàn)阿里巴巴排名查詢
- Python2.7基于淘寶接口獲取IP地址所在地理位置的方法【測試可用】
- python實現(xiàn)二維碼掃碼自動登錄淘寶
- Python模擬登陸淘寶并統(tǒng)計淘寶消費(fèi)情況的代碼實例分享
- 通過抓取淘寶評論為例講解Python爬取ajax動態(tài)生成的數(shù)據(jù)(經(jīng)典)
- Python抓取淘寶下拉框關(guān)鍵詞的方法
- python實現(xiàn)爬取千萬淘寶商品的方法
- 簡單的抓取淘寶圖片的Python爬蟲
- Python使用淘寶API查詢IP歸屬地功能分享
- Python3編程實現(xiàn)獲取阿里云ECS實例及監(jiān)控的方法
相關(guān)文章
Python實現(xiàn)讀取大量Excel文件并跨文件批量計算平均值
這篇文章主要為大家詳細(xì)介紹了如何利用Python語言,實現(xiàn)對多個不同Excel文件進(jìn)行數(shù)據(jù)讀取與平均值計算的方法,感興趣的可以了解一下2023-02-02Python 限制線程的最大數(shù)量的方法(Semaphore)
今天小編就為大家分享一篇Python 限制線程的最大數(shù)量的方法(Semaphore),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02Django objects的查詢結(jié)果轉(zhuǎn)化為json的三種方式的方法
這篇文章主要介紹了Django objects的查詢結(jié)果轉(zhuǎn)化為json的三種方式的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11Python內(nèi)置函數(shù)bin() oct()等實現(xiàn)進(jìn)制轉(zhuǎn)換
使用Python內(nèi)置函數(shù):bin()、oct()、int()、hex()可實現(xiàn)進(jìn)制轉(zhuǎn)換;先看Python官方文檔中對這幾個內(nèi)置函數(shù)的描述,需要了解的朋友可以參考下2012-12-12在arcgis使用python腳本進(jìn)行字段計算時是如何解決中文問題的
這篇文章主要介紹了在arcgis使用python腳本進(jìn)行字段計算時是如何解決中文問題的,需要的朋友可以參考下2015-10-10使用python執(zhí)行shell腳本 并動態(tài)傳參 及subprocess的使用詳解
這篇文章主要介紹了使用python執(zhí)行shell腳本 并動態(tài)傳參 及subprocess的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03