Python one-day training 05 ---- furious clicks, python05 ----

Source: Internet
Author: User

Python one-day training 05 ---- furious clicks, python05 ----


Automatically obtain the CSDN article list and add clicks for each article.

Source code
Import urllib. requestimport reimport timeimport randomfrom bs4 import BeautifulSoupp = re. compile ('/a359680405/article/details /........ ') # Your own blog homepage url = "" # using build_opener () is to let the python program mimic the browser to access opener = urllib. request. build_opener () opener. addheaders = [('user-agent', 'mozilla/100')] html = opener. open (url ). read (). decode ('utf-8') allfinds = p. findall (html) print (allfinds) urlBase = "" # part of the URL that needs to be merged # The URL on the page is duplicated, use set to repeat mypages = list (set (allfinds) for I in range (len (mypages )): mypages [I] = urlBase + mypages [I] print ('the page to be refreshed is: ') for index, page in enumerate (mypages): print (str (index ), page) # set the number of times each page needs to be refreshed brushMax = 200 # print ('click here to brush it: ') for index, page in enumerate (mypages ): brushNum = random. randint (0, brushMax) for j in range (brushNum): try: pageContent = opener. open (page ). read (). decode ('utf-8') # Use BeautifulSoup to parse the title soup = BeautifulSoup (pageContent) blogTitle = str (soup. title. string) blogTitle = blogTitle [0: blogTitle. find ('-')] print (str (j), blogTitle) failed t urllib. error. HTTPError: print ('urllib. error. HTTPError ') time. sleep (1) # an error occurs. wait a few seconds before calling urllib. error. URLError: print ('urllib. error. URLError ') time. sleep (1) # an error occurs. wait a few seconds for the first time. sleep (0.1) # normal pause to avoid server access rejection


Related Article

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: 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.