脚本实现:获取51cto网站某大牛文章的url,并存储到数据库中。
#!/usr/bin/env python #coding:utf-8 from bs4 import BeautifulSoup import urllib import re import MySQLdb k_art_name = [] v_art_url = [] db = MySQLdb.connect(‘192.168.115.5‘,‘blog‘,‘blog‘,‘blog‘) cursor = db.cursor() for page in range(1,5): page = str(page) url = ‘http://yujianglei.blog.51cto.com/all/7215578/page/‘ + page request = urllib.urlopen(url) response = request.read() response = unicode(response,‘GBK‘).encode(‘UTF-8‘) soup = BeautifulSoup(response,‘html.parser‘) a_tag = soup.find_all(href=re.compile("^/\d{7,}/\d{7,}$")) for i in a_tag: art_name = i.string art_url = ‘http://yujianglei.blog.51cto.com‘ + i[‘href‘] k_art_name.append(art_name) v_art_url.append(art_url) if len(k_art_name) == len(v_art_url): number1 = len(k_art_name) number2 = len(v_art_url) for j in range(number1): a = k_art_name[j] b = v_art_url[j] print a,b sql = ‘‘‘INSERT INTO blog_51cto (art_name,art_url) VALUES ("%s","%s")‘‘‘ %(a,b) try: cursor.execute(sql) db.commit() except Exception, e: print e db.rollback() else: print ‘k_art_name:‘, len(k_art_name) print ‘v_art_url:‘ , len(v_art_url) cursor.close() db.close() # insert into blog_51cto (art_name,art_url) VALUES ("oracle 手动归档、自动归档、归档位置、归档进程","http://yujianglei.blog.51cto.com/7215578/1560485") #CREATE DATABASE blog; #CREATE TABLE `blog_51cto` ( # `id` int(11) NOT NULL AUTO_INCREMENT, # `art_name` varchar(500) DEFAULT NULL, # `art_url` varchar(500) DEFAULT NULL, # PRIMARY KEY (`id`) # ) ENGINE=InnoDB AUTO_INCREMENT=1609 DEFAULT CHARSET=utf8;
时间: 2024-10-20 06:16:20