crawler碎碎念4 关于python requests、Beautiful Soup库、SQLlite的基本操作
Requests
import requests from PIL import Image from io improt BytesTO import jason url = "..........." print(dir(requests)) #查看基本的用法 r = requests.get(url) print(r.text) print(r.status_code) print(r.encoding)
传递参数
params = {'k1':'v1','k2':'v2','k3':[1,2,3],'k4':None} #key的值是list的话就会一一赋值
r = requests.get('http://httpbin.org/get',params)
print(r.url)
二进制数据
r= requests.get('.........')
image = Image.open(BytesTO(r.content))
image.save('图片.jpg')
json处理
r = requests.get('https://github.com/timeline.jason')
print(type(r.json))
print(r.json)
print(r.text)
原始数据处理
r= requests.get('.........')
with open('图片.jpg','wb+') as f :
for chunk in r.iter_content(1024):
f.write(chunk)
提交表单
form = {‘username’:‘xxx’,'ddddefsc':'dscdsc'}
r = requests.post('http://httpbin.org/post',data = form)
r = requests.post('http://httpbin.org/post',data = jason.dumps(forms))
print(r.text)
cookies
url ='xxxxxxxxxxxx'
r = requests.get(url)
cookies = r.cookies
for k,v in cookies.get_dict().items(): 标准的获取cookies
print(k,,v)
cookies = {'c1':'v1'}
r = requests.get('http://httpbin.org/cookies',cookies= cookies)
print(r.text)
重定向和重定向历史 网站跳转的时候跟踪用
r= requests.head('http://www.baidu.com',allow_redirects = True)
print(r.url)
print(r.status_code)
print(r.history)
代理
proxies = {'http':'...','https:'.....'} #可以用来科学上网嘻嘻
r = requests.get('http://httpbin.org/cookies',proxies= proxies)
Beautiful Soup
from bs4 import BeautifulSoup
#Tag
soup = Beautifulsoup(open('test.html'))
print(soup.prettify())
print(soup.title.name)
print(soup.title)
#String
print(type(soup.title.string))
print(soup.title.string)
#Comment注释
print(type(soup.a.string))
print(soup.a.name) for items in soup.body.contents:
print(item.name)
#只找子元素的 css查询
print(soup.select('.sister')) #返回到是数组
print(soup.select('a'))
print(soup.select('#link'')) #从id开始找 print(soup.select('head >title''))
Htmlparser
from HTMLParser import HTMLParser clase MyParser(HTMLParser):
def handle_decl(self,decl):
HTMLParser.handle_decl(self,decl)
print('decl %s'% decl) def handle_starttag(self,tag,attrs):
HTMLParser.handle_starttag(self,tag,attrs)
print('<'+tag+'>') def handle_endtag(self,decl):
HTMLParser.handle_endtag(self,decl)
print('<'+tag+'>')
def handle_data(self,data):
HTMLParser.handle_data(self,data)
print('data %s',data)
def handle_startendtag(self,tag,attrs):
HTMLParser.handle_startendtag(self,tag,attrs)
print('<'+tag+ '>')
def handle_comment(self,data):
HTMLParser.handle_comment(self,data)
print('data %s',data) def close(self):
HTMLParser.close(self)
print('Close')
demo = MyParser()
demo.feed(open('hello.html')).read()
demo.close
html格式的尽量不要用xml的方式去处理,因为html可能格式不完整
sqlite3
import sqlite3
conn =sqlite3.connect('test.db')
create_sql = 'create table company(id int primary key not null,emp_name text not null );'
conn.execute(create_sql)
insert_sql = 'insert into company values(?,?)'
conn.execute(insert_sql,(100,'LY'))
conn.execute(insert_sql,(200,'July'))
cursors = conn.execute('select id,emp_name from company')
for row in cursors:
print(row[0],row[1])
conn.close()
mySQL
需要指定mysql:host(ip/port),username,password,
然后在插入数据后要记得使用conn.commit
crawler碎碎念4 关于python requests、Beautiful Soup库、SQLlite的基本操作的更多相关文章
- python之Beautiful Soup库
1.简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索 ...
- Python Beautiful Soup库
Beautiful Soup库 Beautiful Soup库:https://www.crummy.com/software/BeautifulSoup/ 安装Beautiful Soup: 使用B ...
- Python之Beautiful Soup 4使用实例
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库,它能够通过你喜欢的转换器实现惯用的文档导航.查找.修改文档的方式.Beautiful Soup 4 官方文档: ...
- Python之Beautiful Soup的用法
1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...
- python beautiful soup库的超详细用法
原文地址https://blog.csdn.net/love666666shen/article/details/77512353 参考文章https://cuiqingcai.com/1319.ht ...
- Python的Beautiful Soup简单使用
Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能 它是一个工具箱, ...
- 【python】Beautiful Soup的使用
1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...
- 【Python爬虫学习笔记(3)】Beautiful Soup库相关知识点总结
1. Beautiful Soup简介 Beautiful Soup是将数据从HTML和XML文件中解析出来的一个python库,它能够提供一种符合习惯的方法去遍历搜索和修改解析树,这将大大减 ...
- python Beautiful Soup库入门
bs4库的HTML内容遍历方法 基于bs4库的HTML格式输出 显示:友好的显示 <tag>.prettify() 编码:bs4库将任何HTML输入都变成utf-8编码(python 3. ...
随机推荐
- ZR1050
ZR1050 http://www.zhengruioi.com/problem/1030 题目大意: 给定一棵带点权的树,求所有联通块的点权和的平方的和 \(n \le 10^5\) 题解 首先,关 ...
- js的cookie操作及知识点详解
<html> <head> <script type="text/javascript"> function getCookie(c_name) ...
- Linux 内核kobject非 缺省属性
在许多情况中, kobject 类型的 default_attrs 成员描述所有的 kobject 会拥有的属性. 但是那不是一个设计中的限制; 属性随意可以添加到和删除自 kojects. 如果你想 ...
- dotnet core 使用 PowerShell 脚本
本文告诉大家如何在 dotnet core 通过 Host PowerShell 的方法使用 PowerShell 脚本 本文提供的方法需要在 dotnet core 2.1 和以上的版本,对于 do ...
- How to fix nuget Unrecognized license type MIT when pack
When I packaging license within the nupkg, I will using License to replace licentUrl. I using this c ...
- Excel基本功能
公式基础: 比较运算符的种类 flase对应0 而ture对应1 连接运算 利用之前提到的ture就是1 乘以100 注意用括号区分优先级 函数应用基础: 系统已经列好这几个常用的函数 右键单击状态栏 ...
- 第二阶段:2.商业需求分析及BRD:2.产品需求池
需求获取方式 比如公司战略方面的需求 用户的反馈:投诉 建议等等 产品经理需要时刻关注竞品以及行业的发展! 需求池:各个产品经理的需求总和成一个需求池.让资源更好的利用起来.有的公司还有个“需求管理 ...
- 剑指Offer-62.数据流中的中位数(C++/Java)
题目: 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值.我们使 ...
- 【重学Node.js 第1&2篇】本地搭建Node环境并起RESTful Api服务
本地搭建Node环境并起RESTful Api服务 课程介绍看这里:https://www.cnblogs.com/zhangran/p/11963616.html 项目github地址:https: ...
- 你的IDEA过期了?跃哥四大招帮你稳住
作者:Dimple Solgan:当你的才华还无法撑起你的野心时候,那应该静下心来好好学习 前天晚上在群里风风火火组建了两个学习小组,一个是面向Java初学,一个是面向Python初学,把我搞的兴奋不 ...