路飞学城-Python爬虫实战密训-第1章

Posted on 2018-07-06 18:43 夜市街 阅读() 评论() 编辑 收藏

这是我第一篇博客

在前几天,参加了路飞学城的Python爬虫培训,结束了第一章节的课程,有些想法和心得在这里记录下来。

 requests模块有常用的get,post,和put方法。

requests模块的requeset函数

requests.request()

– method:提交方式,post,get,delete, put, head, patch, options
– url: 提交地址
– params: 在url中传递参数,GET params = {k:v}
– data: 在请求体里传递参数用于post请求 
– json: 在请求体里传递参数,并在请求头中设置content-type: application/json
– headers: 在请求头中添加数据
– cookies: 网站cookies 在请求头中
– auth : 认证使用 在 请求头中加入用户名密码
– timeout : 超时时间
– allow_redirects: 是否允许重定向 bool
– proxies: 代理
– stream: 流,bool 用于下载文件
ret = request.get(‘http://127.0.0.1:6666/me/’, steam=True)
for i in ret.iter.content():
  print(i)
– cert: 证书 指定https SSL证书文件
– verify = False https忽略证书存在

 

而BeautifulSoup模块自己总结了一点

  1 from bs4 import BeautifulSoup
  2 from bs4.element import Tag
  3 
  4 html_doc = """
  5 <html><head><title>The Dormouse's story</title></head>
  6 <body>
  7     <div class="title">
  8         <b>The Dormouse's story总共</b>
  9         <h1>f</h1>
 10     </div>
 11 
 12 </body>
 13 </html>
 14 """
 15 soup = BeautifulSoup(html_doc, "lxml")
 16 title = soup.find(name="body")
 17 Tag = Tag(name="a", attrs={"id": "cc"})
 18 Tag.string = "new"
 19 # 筛选标签
 20 # children 后代标签
 21 # print(list(title.children))
 22 # ['\n', <b>The Dormouse'<a>test</a>s story总共</b>, '\n', <h1>f</h1>, '\n']
 23 
 24 # descendants 后代所有标签 print(list(title.descendants)) ['\n', <b>The Dormouse'<a>test</a>s story总共</b>, "The Dormouse'",
 25 #  <a>test</a>, 'test', 's story总共', '\n', <h1>f</h1>, 'f', '\n']
 26 
 27 # clear 删除所有标签,留下自己
 28 # decompose 删除所有标签,自己也不留
 29 # extract 删除所有标签,获取被删除的
 30 
 31 # decode 转换为字符串,含当前标签
 32 # print(title.decode)
 33 # encode_contents 转换为字符串,不含当前标签
 34 # print(title.decode.contents)
 35 
 36 # find 获取匹配的第一个标签 可加参数 recursive=True 是否递归找子孙
 37 # print(title)
 38 # find_all 获取匹配的所有标签,可加参数limit 取几个 name就是可以找到的
 39 # has_attr 检查是否有这个属性
 40 # v = title.has_attr("id")
 41 # print(v)
 42 # get_text 获取标签内部文本内容
 43 # v = title.get_text("h1")
 44 # print(v)
 45 # index 检查标签在某标签的索引位置
 46 # is_empty_element 是否为空标签或者是单标签
 47 # 获取当前的关联标签
 48 
 49 # 早上填坑
 50 # soup.next
 51 # soup.next_element
 52 # soup.next_elements
 53 # soup.next_sibling
 54 # soup.next_siblings
 55 # tag.previous
 56 # tag.previous_element
 57 # tag.previous_elements
 58 # tag.previous_sibling
 59 # tag.previous_siblings
 60 # tag.parent
 61 # tag.parents
 62 
 63 # 获取某标签的关联标签
 64 # tag.find_next(...)
 65 # tag.find_all_next(...)
 66 # tag.find_next_sibling(...)
 67 # tag.find_next_siblings(...)
 68 # tag.find_previous(...)
 69 # tag.find_all_previous(...)
 70 # tag.find_previous_sibling(...)
 71 # tag.find_previous_siblings(...)
 72 # tag.find_parent(...)
 73 # tag.find_parents(...)
 74 # 参数同find_all
 75 
 76 # select
 77 # title.String可以改内容,文本替换
 78 # title.stripped_strings #递归获取所有文本
 79 # v = title.stripped_strings
 80 # print(list(v))
 81 
 82 # append 追加一个标签,相当于剪切
 83 print(title)
 84 print("-" * 10)
 85 title.append(Tag)
 86 print(title)
 87 print("-" * 10)
 88 # Tag = Tag(name="a",attrs={"id":"cc"})
 89 # Tag.string="new"
 90 
 91 # insert在当前标签内部指定位置插入一个标签
 92 # from bs4.element import Tag
 93 # obj = Tag(name='i', attrs={'id': 'it'})
 94 # obj.string = '我是一个新来的'
 95 # tag = soup.find('body')
 96 # tag.insert(2, obj)
 97 # print(soup)
 98 
 99 # insert_after,insert_before 在当前标签后面或前面插入
100 # from bs4.element import Tag
101 # obj = Tag(name='i', attrs={'id': 'it'})
102 # obj.string = '我是一个新来的'
103 # tag = soup.find('body')
104 # # tag.insert_before(obj)
105 # tag.insert_after(obj)
106 # print(soup)

View Code

 

基于一开始学习的Requests和BeautifulSoup,在学习这门课程之前,已经了解过这两个模块,所以听起来并不费力,不过对于爬虫的这方面的思路还不是特别明确,不知道的先做什么后做什么。

不过好在现在明确了。

对于登录 我认为有个明确的步骤

find和find_all的区别是后者返回一个列表

根据需求选择get和post方法的使用,GET的参数是在HTTP 的头部传送的,而Post的数据则是在HTTP 请求的内容里传送

requests模块还有一些不可以忘记的

比如:method,url,params。data,json,headers,cookies,这些都已经记在脑中。

files,auth,timeout等是上传文件,认证和超时,以后会用得上。

 

版权声明:本文为Ojia原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Ojia/p/9275162.html