當前位置: 首頁>>代碼示例>>Python>>正文


Python Request.headers["User-Agent"]方法代碼示例

本文整理匯總了Python中scrapy.http.request.Request.headers["User-Agent"]方法的典型用法代碼示例。如果您正苦於以下問題:Python Request.headers["User-Agent"]方法的具體用法?Python Request.headers["User-Agent"]怎麽用?Python Request.headers["User-Agent"]使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在scrapy.http.request.Request的用法示例。


在下文中一共展示了Request.headers["User-Agent"]方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: parse

# 需要導入模塊: from scrapy.http.request import Request [as 別名]
# 或者: from scrapy.http.request.Request import headers["User-Agent"] [as 別名]
 def parse(self, response):
     #obtains links from page to page and passes links to parse_playerURL
     sel = Selector(response)    #define selector based on response object (points to urls in start_urls by default) 
     url_list = sel.xpath('//tbody/tr/td[@class="player"]/a/@href')   #obtain a list of href links that contain relative links of players
     
     for i in url_list:
         relative_url = self.clean_str(i.extract())    #i is a selector and hence need to extract it to obtain unicode object
         print urljoin(response.url, relative_url)   #urljoin is able to merge absolute and relative paths to form 1 coherent link
         req = Request(urljoin(response.url, relative_url),callback=self.parse_playerURL)   #pass on request with new urls to parse_playerURL
         req.headers["User-Agent"] = self.random_ua()    
         yield req
     
     next_url=sel.xpath('//div[@class="right-nav pull-right"]/a[@rel="next"]/@href').extract_first()  
     if(next_url):                                                                       #checks if next page exists
         clean_next_url = self.clean_str(next_url)
         reqNext = Request(urljoin(response.url, clean_next_url),callback=self.parse)    #calls back this function to repeat process on new list of links
         yield reqNext
開發者ID:HashirZahir,項目名稱:FIFA-Player-Ratings,代碼行數:19,代碼來源:fifa_spider.py


注:本文中的scrapy.http.request.Request.headers["User-Agent"]方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。