国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python查詢阿里巴巴關鍵字排名的方法

2019-11-25 17:13:09
字體:
來源:轉載
供稿:網友

本文實例講述了Python查詢阿里巴巴關鍵字排名的方法。分享給大家供大家參考。具體如下:

這里使用python庫urllib及pyquery基本東西的應用,實現阿里巴巴關鍵詞排名的查詢,其中涉及到urllib代理的設置,pyquery對html文檔的解析

1. urllib 基礎模塊的應用,通過該類獲取到url中的html文檔信息,內部可以重寫代理的獲取方法

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. 根據輸入的公司名及關鍵詞列表,返回每個關鍵詞的排名

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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新竹市| 会同县| 三河市| 尉氏县| 育儿| 闵行区| 伊宁市| 乌拉特后旗| 吴堡县| 永康市| 双鸭山市| 英吉沙县| 云林县| 公主岭市| 莱芜市| 荆州市| 双牌县| 吉林市| 山丹县| 伊宁县| 崇明县| 马山县| 阿尔山市| 巨鹿县| 新密市| 静宁县| 定州市| 正镶白旗| 通州市| 萨迦县| 阿克陶县| 新平| 类乌齐县| 攀枝花市| 延津县| 云南省| 扎赉特旗| 涟水县| 图们市| 兰州市| 正宁县|