Requests crawl Baidu paste: Python beauty 3

Source: Internet
Author: User

Import Requestsimport Sysclass Tieba (object): Def __init__ (self, tieba_name, pn): Self.tieba_name = Tieba_name Self.base_url = ' https://tieba.baidu.com/f?kw=%spn= '% (tieba_name) # print (Self.base_url) self.url_li        st = [] for i in range (pn): url = self.base_url + str (i *) self.url_list.append (URL) # print (self.url_list) self.headers = {' user-agent ': ' mozilla/5.0 (Windows NT 6.1; Win64;  x64) applewebkit/537.36 (khtml, like Gecko) chrome/62.0.3202.89 safari/537.36 '} Pass # Gets the response for the URL def    Get_page (self, url): Response = Requests.get (URL, headers=self.headers) return Response.content # Save data def save_content (self, Content, index): filename = self.tieba_name + ' _ ' + str (index) + '. html ' with open (        FileName, ' WB ') as F:f.write (content) def run (self): # build base URL # Build URL list # Build request Header # traverse the URL list for the URL in self.uRl_list: # Send Request content = Self.get_page (URL) # Save response index = Self.url_list.inde X (URL) self.save_content (content, index) passif __name__ = = ' __main__ ': name = sys.argv[1] pn = sy S.ARGV[2] Tieba = Tieba (name, int (PN)) Tieba.run ()

  

How to use

Python code file name Beauty 3

Requests crawl Baidu paste: Python beauty 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.