import urllib.request as ul
import re
from bs4 import BeautifulSoup
import time
import random


class HotBlog(object):
    url_pattern = "http://www.baidu.com/s?ie=utf-8&pn={pn}&wd={wd}"

    def get_page(self, url):
        contents = b""
        try:
            page = ul.urlopen(url, timeout=5)
            contents = page.read()
        except Exception:
            print("Connection timeout!")
        return contents.decode("utf-8")

    def scan_page(self, pn, wd):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
        html = self.get_page(self.url_pattern.format(pn=pn, wd=wd))
        soup = BeautifulSoup(html, "lxml")
        blog_title_pattern = re.compile(".*- DRFish - 博客频道 - CSDN.NET$")
        for target in soup.find_all(id=re.compile("tools_[0-9]*_[1-9]")):
            data_tools = target.attrs["data-tools"]
            parts = data_tools.split('","url":"')
            if len(parts) != 2:
                continue
            title = parts[0][10:]
            url = parts[1][:-2]

            if re.match(blog_title_pattern, title):
                random.seed(time.time())
                time.sleep(random.uniform(random.random() * 2, random.random() * 50))
                request = ul.Request(url, headers=headers)
                ul.urlopen(request)
                print("visit:" + title)

    def scan_n_pages(self, n, wd):
        for i in range(n):
            self.scan_page(10 * i, wd)
@@ -179,16 +179,146 @@
 wd=%7Bwd%7D%22
+%0A    headers = %7B%0A        'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'%7D
 %0A%0A    de
@@ -593,154 +593,26 @@
 lf, 
-p
 n, wd
-):%0A        headers = %7B%0A            'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'%7D
+, username):%0A
 %0A   
@@ -664,17 +664,21 @@
 rmat(pn=
-p
+10 * 
 n, wd=wd
@@ -772,14 +772,18 @@
 .*- 
-DRFish
+%7Busername%7D
  - %E5%8D%9A
@@ -798,16 +798,99 @@
 DN.NET$%22
+.format(username=username))%0A        print(%22----- %E5%BC%80%E5%A7%8B%E8%AE%BF%E9%97%AE%E7%AC%AC%22 + str(n + 1) + %22%E9%A1%B5%E6%90%9C%E7%B4%A2%E7%BB%93%E6%9E%9C-----%22
 )%0A      
@@ -1412,16 +1412,21 @@
 headers=
+self.
 headers)
@@ -1501,16 +1501,73 @@
 + title)
+%0A        print(%22----- %E7%BB%93%E6%9D%9F%E8%AE%BF%E9%97%AE%E7%AC%AC%22 + str(n + 1) + %22%E9%A1%B5%E6%90%9C%E7%B4%A2%E7%BB%93%E6%9E%9C-----%22)
 %0A%0A    de
@@ -1592,16 +1592,26 @@
 f, n, wd
+, username
 ):%0A     
@@ -1663,15 +1663,336 @@
 age(
-10 * i, wd)
+i, wd, username)%0A%0A    def interpret(self):%0A        print(%22%E8%BF%99%E4%B8%AA%E8%84%9A%E6%9C%AC%E5%8F%AF%E4%BB%A5%E9%80%9A%E8%BF%87%E7%99%BE%E5%BA%A6%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E%E6%9D%A5%E8%AE%BF%E9%97%AECSDN%E5%8D%9A%E5%AE%A2%EF%BC%8C%E6%8F%90%E9%AB%98%E5%8D%9A%E5%AE%A2%E6%A3%80%E7%B4%A2%E6%8E%92%E5%90%8D%E3%80%82%22)%0A        username = input(%22%E8%AF%B7%E8%BE%93%E5%85%A5%E4%BD%A0%E7%9A%84CSDN%E7%94%A8%E6%88%B7%E5%90%8D%EF%BC%9A%5Cn%22)%0A        key = input(%22%E8%AF%B7%E8%BE%93%E5%85%A5%E6%90%9C%E7%B4%A2%E5%85%B3%E9%94%AE%E8%AF%8D%EF%BC%9A%5Cn%22)%0A        page_count = int(input(%22%E8%AF%B7%E8%BE%93%E5%85%A5%E4%BD%A0%E6%83%B3%E8%A6%81%E6%90%9C%E7%B4%A2%E7%9A%84%E9%A1%B5%E9%9D%A2%E6%95%B0%EF%BC%9A%5Cn%22))%0A        self.scan_n_pages(page_count, key, username)%0A%0A%0Aif __name__ == %22__main__%22:%0A    HotBlog().interpret()%0A

Add interpreter for HotBlog.