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的基本操作的更多相关文章

  1. python之Beautiful Soup库

    1.简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索 ...

  2. Python Beautiful Soup库

    Beautiful Soup库 Beautiful Soup库:https://www.crummy.com/software/BeautifulSoup/ 安装Beautiful Soup: 使用B ...

  3. Python之Beautiful Soup 4使用实例

    Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库,它能够通过你喜欢的转换器实现惯用的文档导航.查找.修改文档的方式.Beautiful Soup 4 官方文档: ...

  4. Python之Beautiful Soup的用法

    1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...

  5. python beautiful soup库的超详细用法

    原文地址https://blog.csdn.net/love666666shen/article/details/77512353 参考文章https://cuiqingcai.com/1319.ht ...

  6. Python的Beautiful Soup简单使用

    Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能 它是一个工具箱, ...

  7. 【python】Beautiful Soup的使用

    1. Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...

  8. 【Python爬虫学习笔记(3)】Beautiful Soup库相关知识点总结

    1. Beautiful Soup简介     Beautiful Soup是将数据从HTML和XML文件中解析出来的一个python库,它能够提供一种符合习惯的方法去遍历搜索和修改解析树,这将大大减 ...

  9. python Beautiful Soup库入门

    bs4库的HTML内容遍历方法 基于bs4库的HTML格式输出 显示:友好的显示 <tag>.prettify() 编码:bs4库将任何HTML输入都变成utf-8编码(python 3. ...

随机推荐

  1. P1052 国王放置问题

    题目描述 在n*m的棋盘上放置k个国王,要求k个国王互相不攻击,有多少种不同的放置方法.假设国王放置在第(x,y)格,国王的攻击的区域是:(x-1,y-1), (x-1,y),(x-1,y+1),(x ...

  2. excel转换成实体

    package com.cinc.ecmp.utils; import java.io.IOException; import java.io.InputStream; import java.lan ...

  3. 【41.34%】【BZOJ 1003】[ZJOI2006]物流运输

    Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 6420  Solved: 2654 [Submit][Status][Discuss] Descri ...

  4. records

    2019年数据地址备份: three.js 实例在NextWebProject/static/canvas下边! qlgj 在NextWebProject下边!

  5. Laravel -- Excel 导入(import) (v2.1.0)

    原博客地址 https://www.jianshu.com/p/7287ebdc77bb Install (安装) //> composer.json 中 require中添加如下: " ...

  6. Jmeter配置元件——CSV DataSet Config参数化

    在聊CSV DataSet Config配置元件前,先来讨论下为何要参数化? 比如在做性能测试过程中, 一般我们需要模拟多个用户进行操作, 为了满足实际场景, 模拟真实的用户行为, 我们需要做到模拟的 ...

  7. C# 发送电子邮件(smtp)

    相关享目托管在github: https://github.com/devgis/CSharpCodes

  8. Python13_安装、解释器

    Linux下大部分系统默认自带python2.x的版本,最常见的是python2.6或python2.7版本,默认的python被系统很多程序所依赖, 比如centos下的yum就是python2写的 ...

  9. 一点资讯 视频抓取 phantomjs

    # _*_ coding: utf- _*_ """ 思路: .列表页使用phantomjs模拟点击.每个链接只抓取第一页9-10条内容,按照标题去重 .布置定时任务,每 ...

  10. 2020年. NET Core面试题

    第1题,什么是ASP net core? 首先ASP net core不是 asp net的升级版本.它遵循了dot net的标准架构, 可以运行于多个操作系统上.它更快,更容易配置,更加模块化,可扩 ...