欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python查詢阿里巴巴關(guān)鍵字排名的方法

 更新時間:2015年07月08日 11:52:10   作者:小剛1  
這篇文章主要介紹了Python查詢阿里巴巴關(guān)鍵字排名的方法,涉及Python基于urllib模塊解析html頁面及進(jìn)行URL查詢的相關(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è)計有所幫助。

相關(guān)文章

最新評論