bs4解析网页、lxml+xpath解析网页、xlwt存储到Excel、sqlite3存储到数据库

bs4解析网页

from urllib.request import Request,urlopen
from bs4 import BeautifulSoup

html = urlopen(url="http://www.baidu.com").read()  #得到二进制的html文本
soup = BeautifulSoup(html, "html.parser")   #将接收到的html转换成BeautifulSoup对象


print("获取title文本内容:",soup.title.string)
print("只获取第一个a标签:",soup.a)


#得到全部的a标签 首先需要for循环遍历然后.string的到文本
a = soup.find_all("a")    #也可再加一个参数limit进行分行数
for i in a:
    print(i.string)    #可以获取所有a标签的文本内容

at = soup.find_all(["a","title"])    #用[]增加参数,如果去掉中括号就是筛选出两个参数都符合的内容
for i in at:
    print(i)

# -----------kwargs参数 首先for循环 .text参数得到文本
head = soup.find_all(id="head") #根据id名获取整个标签体
for i in head:
    print(i.text)

class2 = soup.find_all(class_="s-top-wrap s-isindex-wrap") #class 要加_区分
print(class2)

#使用正则表达式,找出包含这个连接的标签
import re
#符合正则表达式规则:含有这个url的href标签都会被打印出来
t_list = soup.find_all(href=re.compile("http://www.baidu.com"))
for i in t_list:
    print(i.text)

#一些特殊的属性需要自己通过dict格式查找
t_list2 = soup.find_all(attrs={"rel":"dns-prefetch"})
for item in t_list2:
    print(item)

#-----------选择器
print(soup.select('title')[0].text)
print(soup.select('a'))

#通过类名查找
print(soup.select('.mnav'))

#通过id查找
print(soup.select('#u1'))

#组合查找
print(soup.select('div .bri'))

#属性查找
print(soup.select('a[class="bri"]'))
print(soup.select('a[href="http://www.baidu.com"]'))

#直接子标签查找
t_list = soup.select("head > title")
print(t_list)

#兄弟节点标签查找
t_list = soup.select(".mnav ~ .bri")
print(t_list)

#正则提取
t_list = soup.select("title")
print(soup.select('title')[0].get_text())

lxml+xpath解析网页

import requests
from lxml import etree

html = requests.get("https://www.bilibili.com/").text
etree_html = etree.HTML(html)   #将string转换为Element

#通过xpath获取到一个视频的网址
xpath = etree_html.xpath('//*[@id="app"]/div/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/a//@href')
i = 'https:'+xpath[0]   #得到网址
print(i)

这个插件是xpath helper
在这里插入图片描述

xlwt存储到Excel

注意:Excel的坐标是从(0,0)开始的

import xlwt

workbook = xlwt.Workbook(encoding="utf-8")  #创建workbook对象
sheet = workbook.add_sheet("sheet1",cell_overwrite_ok=True)    #创建一个表,允许覆盖
sheet.write(0,0,'hello')    #行,列,数据
workbook.save("测试Excel.xls")    #注意:保存的时候不要打开excel不然报错‘w+b’

sqlite3存储到数据库

注意:如果设置自增长,要用intger不要用int,格式如下:
id integer primary key autoincrement not null,

import sqlite3


#-----1、创建一个数据库和数据库对象
connect = sqlite3.connect("test.db")    #test.db如果不存在就会被创建

#-----2、得到一个游标,用于执行sql语句
cursor = connect.cursor()

#-----3、写sql语句

#-----3.1创建一个表 三个单引号可以直接写内容
# sql = '''
#     create table test(
#     id  int primary key not null,
#     name varchar(20),
#     age int )
# '''

#-----3.1想表中添加数据
sql = '''
    insert into test(id,name,age)
    values(11,'呵呵',32),
    (12,'哈哈',33),
    (13,'拉了',34)
'''

#-----4、执行sql语句
execute = cursor.execute(sql)

#-----5、提交
connect.commit()
#-----6、关闭
connect.close()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值