Spider爬虫基础
get获取某个网站的html代码,post访问网站获取网站返回的信息
import urllib.request
import urllib.parse
#使用get请求
def start1():
response=urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))
#使用post请求
def start2():
data=bytes(urllib.parse.urlencode({'dsadasdas':'杀马特'}),encoding='utf8')
#使用uillib.parse来将想发送的表单的键值对按照utf8弄成适合网页post传输的形式
response=urllib.request.urlopen('http://httpbin.org/post',data=data)
print(response.read().decode('utf-8'))
start2()
设置访问超时处理
import urllib.request
import urllib.parse try:
response=urllib.request.urlopen('http://www.baidu.com',timeout=0.01)
print(response.read().decode('utf-8'))
except urllib.error.URLError as e:
print('time out')
获取状态码等
import urllib.request
import urllib.parse response=urllib.request.urlopen('http://www.baidu.com')
print(response.getheader) #获取请求的信息头
print(response.status) #获取请求的状态码
response=urllib.request.urlopen('http://douban.com')
print(response.status) #出现418状态码表示自己被发现是爬虫了
通过发送头部来伪装浏览器,突破豆瓣
import urllib.request
import urllib.parse url1='http://www.douban.com'
sendheader= {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.67"
}
req=urllib.request.Request(url=url1,headers=sendheader)
response=urllib.request.urlopen(req)
print(response.read().decode('utf-8'))
beatifulsoup4的使用
bs4可以将复杂的html文档转换成一个复杂的树形结构,每个节点都是python对象,所有的对象可以归纳成4种
-Tag
-NavigableString
-Comment
文档的便利,文档的搜索,css选择器
import urllib.request
import urllib.parse
import re
from bs4 import BeautifulSoup file = open('baidu.html','rb')
html=file.read().decode('utf8')
bs=BeautifulSoup(html,'html.parser')
#1.tag 标签及其内容
print(bs.title) #打印title标签
print(bs.title.string) #打印title里面的字符串内容
print(bs.a.attrs) #拿到标签里面的属性放进字典 #comment 是注释,输出的内容不包括注释符号
#----------文档的便利——---------
print('contents用于遍历某个标签里面的节点放进一个列表',bs.head.contents)
print('可以获取列表里面的某一个值',bs.head.contents[0])
#----------文档的搜索——---------
t_list=bs.find_all('a')
#下面这种是字符串过滤,会找完全匹配的内容
print('找所有的a标签放进列表',t_list)
#下面这种是正则表达式过滤
t_list1=bs.find_all(re.compile('a'))
print('正则找含有a的标签',t_list1)
#还有根据函数(来返回)过滤
def name_exist(tag):
return tag.has_attr('name')
t_list=bs.find_all(name_exist)
print('找出属性中有name的',t_list) t_list=bs.find_all(id='head')
print('找id等于head的',t_list) t_list=bs.find_all(text='百度首页')
print('找文本等于百度首页的',t_list)
#----------css选择器——---------
t_list=bs.select('title') #按照标签查找
print(t_list)
t_list=bs.select('.bdsug') #按照css类查找
print(t_list)
t_list=bs.select('#u1') #按照css的id查找
print(t_list)
t_list=bs.select('head > title') #按照head下面的title查找
print(t_list)
t_list=bs.select('.sda ~ .mm') #找和.sda同一级的.mm 兄弟节点
print(t_list)
t_list=bs.select('div',class_='item') #找div且class是item的。
print(t_list)
保存数据进入xls数据库
import xlwt
workbook=xlwt.Workbook(encoding='utf8',style_compression=0) #创建表对象
worksheet=workbook.add_sheet('sheet1',cell_overwrite_ok=True) #创建工作表,cell_overwrite_ok=True要写,用于后面写的覆盖前面的
worksheet.write(0,0,'hello') #写入数据,第一参数是行,第二个参数是列,第三个参数是内容
col=('链接','图片','关键字')
for i in range(0,3):
worksheet.write(1,i,col[i])
workbook.save('student.xls')
sqlite数据库的使用
import sqlite3
conn = sqlite3.connect('test.db')
c = conn.cursor()
sql='''
create table company
(id int primary key not null,
name text not null,
age int not null,
address char(50),
salary real)
'''
#create table company是创建company的表格
# 下面用括号代表它这个表格里面的内容,首先是id,id是整型且是主键,not nuall是非空
# address char(50)地址是50的字符串 salary是real形的数据 sql1='''
insert into company (id,name,age,address,salary)
values(1,'十大',30,'sjsad',15000) ''' c.execute(sql) #执行sql语句,建表
c.execute(sql1) #执行sql语句,插入
sql3='select id,name,address,salary from company'
#-------以下为查询操作------
cursor=c.execute(sql3)
for row in cursor:
print('id',row[0])
print('name',row[1])
print('address',row[2])
print('salary',row[3])
print('\n')
# -------以上为查询操作------
conn.commit() #提交数据库操作
conn.close() #关闭数据库连接
print('成功建表')
#数据类型 文本text 整形int 字符串型varchar 含小数型numeric
#autoincrement自增长)
Spider爬虫基础的更多相关文章
- Python爬虫基础
前言 Python非常适合用来开发网页爬虫,理由如下: 1.抓取网页本身的接口 相比与其他静态编程语言,如java,c#,c++,python抓取网页文档的接口更简洁:相比其他动态脚本语言,如perl ...
- python 3.x 爬虫基础---Urllib详解
python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 前言 爬虫也了解了一段时间了希望在半个月的时间内 ...
- python 3.x 爬虫基础---常用第三方库(requests,BeautifulSoup4,selenium,lxml )
python 3.x 爬虫基础 python 3.x 爬虫基础---http headers详解 python 3.x 爬虫基础---Urllib详解 python 3.x 爬虫基础---常用第三方库 ...
- spider 爬虫文件基本参数(3)
一 代码 # -*- coding: utf-8 -*- import scrapy class ZhihuSpider(scrapy.Spider): # 爬虫名字,名字唯一,允许自定义 name ...
- java网络爬虫基础学习(三)
尝试直接请求URL获取资源 豆瓣电影 https://movie.douban.com/explore#!type=movie&tag=%E7%83%AD%E9%97%A8&sort= ...
- java网络爬虫基础学习(一)
刚开始接触java爬虫,在这里是搜索网上做一些理论知识的总结 主要参考文章:gitchat 的java 网络爬虫基础入门,好像要付费,也不贵,感觉内容对新手很友好. 一.爬虫介绍 网络爬虫是一个自动提 ...
- python从爬虫基础到爬取网络小说实例
一.爬虫基础 1.1 requests类 1.1.1 request的7个方法 requests.request() 实例化一个对象,拥有以下方法 requests.get(url, *args) r ...
- python爬虫基础_scrapy
其实scrapy想要玩得好,还是需要大量全栈知识的.scrapy 被比喻为爬虫里的django,框架和django类似. 安装: Linux/mac - pip3 install scrapy Win ...
- 爬虫基础以及 re,BeatifulSoup,requests模块使用
爬虫基础以及BeatifulSoup模块使用 爬虫的定义:向网站发起请求,获取资源后分析并提取有用数据的程序 爬虫的流程 发送请求 ---> request 获取响应内容 ---> res ...
随机推荐
- Ubuntu16.04网卡配置
新安装的Ubuntu16.04系统容易出现无法连接有线网络的问题,主要是因为网卡配置不完善,下面通过实操讲解如何解决该问题. 1. 查看网络设备 ifconfig 发现只有enp2s0和lo,没有et ...
- Web项目访问在C盘的图片(不在当前项目路径下的图片)
使用ASPX页面处理 前台显示 <img src="/UeImg.aspx?path=C:/YxFile/ueditor/upload/image/20200211/637170508 ...
- (十二)、file--判断文件类型命令
一.命令描述与格式 描述:linux在查看一个文件前,要首先确定该文件中数据的类型,之后再使用适当的命令或者方法打开该文件,在linux中文件的扩展名并不代表文件的类型,也就是说扩展名与文件的类型没有 ...
- 前置机器学习(五):30分钟掌握常用Matplitlib用法
Matplotlib 是建立在NumPy基础之上的Python绘图库,是在机器学习中用于数据可视化的工具. 我们在前面的文章讲过NumPy的用法,这里我们就不展开讨论NumPy的相关知识了. Matp ...
- HashMap知识点总结,这一篇算是总结的不错的了,建议看看!
HashMap存储结构 内部包含了⼀个 Entry 类型的数组 Entry[] table.transient Entry[] table;(transient:表示不能被序列化)Entry类型存储着 ...
- easyui中刷新列表
<table class="crud-content-info" id="showProductDialogFormstandrad"> </ ...
- C#——时间之不同国家的显示格式
对于时间的显示,不同的地方有不同的时间格式,代码如下: public class Common_DateFormat { public Common_DateFormat() { // // TODO ...
- 不是RESTful不好,是你姿势有问题
文章来源:https://ningyu1.github.io/site/post/01-restful-design-specifications/ 一. 摘要(Abstract) RESTful A ...
- css浅谈
一 CSS文字属性: color : #999999; /*文字颜色*/ font-family : 宋体,sans-serif; /*文字字体*/ font-size : 9pt; /*文字大小*/ ...
- linux based bottlerocket-os
linux based bottlerocket-os 概要 aws开源,专注与运行容器的linux os 参看 https://github.com/bottlerocket-os