首页 技术 正文
技术 2022年11月6日
0 收藏 899 点赞 815 浏览 1659 个字
#python.py
from bs4 import BeautifulSoup
import urllib.request
from MySqlite import MySqliteglobal g_intid
g_intid=0
def GetBlogTileAndName(url):
res = urllib.request.urlopen(url)
html = res.read()
res.close()
str(html, 'utf-8')
soup=BeautifulSoup(html)
divs=soup.find_all(attrs={"class":"postTitle"}) for divname in divs:
print("title:=",divname.a.string,"href:=",divname.a["href"])
global g_intid
g_intid+=1
x=MySqlite()
x.InsertDate(g_intid,divname.a["href"],divname.a.string)
def GetBlogPage(url):
res = urllib.request.urlopen(url)
html = res.read()
res.close()
str(html, 'utf-8')
soup=BeautifulSoup(html)
divPager=soup.find(attrs={"class":"pager"})
print(divPager.string)

for i in range(1,8) :
 url=r”http://www.cnblogs.com/FCoding/default.html?page=”+str(i)
 GetBlogTileAndName(url)

  

#MySqlite.pyclass MySqlite(object):
"""description of class"""
def __init__(self, *args):
return super().__init__(*args)
def callstr(self,str):
print(str) def InsertDate(self,id,url,title):
conn = sqlite3.connect(r"d:\123.db")
c=conn.cursor()
#try:
# c.execute('create table blog (ID intergeer,url text,title text , PRIMARY KEY(ID))')
#except ValueError:
# print("error My")
strExe="insert into blog values ({0}, \"{1}\",\"{2}\")".format(id,url,title)
print(id)
#c.execute('insert into blog values (last_insert_rowid(),url,title)')
c.execute(strExe)
conn.commit()
c.close()
conn.close() def GetDate(self):
import sqlite3
conn = sqlite3.connect(r"d:\123.db")
c=conn.cursor()
res=c.execute("select count(*) from blog")
res=c.fetchone()
print(res[0])
data=c.execute("select * from blog")
for item in data:
for ite in item:
print(ite)
conn.commit()
c.close()
conn.close()

 简述一下功能:

通过urllib 下载网页 使用BeautifulSoup 解析

调用find_all(attrs={“class”:”postTitle”})

找到HTML 中所有class=posttitle 的tag

然后遍历 取出title 和href 保存到数据库中

此程序 无容错。新手无笑!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,982
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,499
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,343
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,126
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,760
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,796