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)京東訂單推送到測試環(huán)境,提供便利操作示例
這篇文章主要介紹了python實現(xiàn)京東訂單推送到測試環(huán)境,提供便利操作,涉及Python基于requests模塊的網(wǎng)絡(luò)請求操作相關(guān)使用技巧,需要的朋友可以參考下2019-08-08
Python 通過監(jiān)聽端口實現(xiàn)唯一腳本運(yùn)行方式
這篇文章主要介紹了Python 通過監(jiān)聽端口實現(xiàn)唯一腳本運(yùn)行方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
pandas實現(xiàn)數(shù)據(jù)合并的示例代碼
本文主要介紹了pandas實現(xiàn)數(shù)據(jù)合并的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
python 將字符串中的數(shù)字相加求和的實現(xiàn)
這篇文章主要介紹了python 將字符串中的數(shù)字相加求和的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python?matplotlib中plt.plot()函數(shù)的顏色參數(shù)設(shè)置及可以直接運(yùn)行的程序代碼
在數(shù)據(jù)可視化中matplotlib.pyplot模塊的plot函數(shù)是一個非常重要且常用的工具,用于繪制2D圖形,這篇文章主要給大家介紹了關(guān)于Python?matplotlib中plt.plot()函數(shù)的顏色參數(shù)設(shè)置及可以直接運(yùn)行的程序代碼,需要的朋友可以參考下2024-03-03
Python?Matplotlib?marker?標(biāo)記詳解
這篇文章主要介紹了Python?Matplotlib?marker?標(biāo)記詳解,Matplotlib,風(fēng)格類似?Matlab?的基于?Python?的圖表繪圖系統(tǒng),詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-07-07

