爬虫学习一

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
一、requests模块GET请求
1.  无参实例
import  requests
ret  =  requests.get( 'http://www.autohome.com.cn/news/' )
ret.encoding = 'gbk'     #改成中文编码
print (ret.url)     #打印url
print (ret.text)     #打印文本
 
2. 有参实例
import  requests
params_dic  =  {
     'hostid' '10107' ,
     'elementid' '23' ,
     'screen' '1' ,
     'name' '10.28.142.240'
}
 
ret  =  requests.get(url = 'http://zabbix.test.com/screens.php' , params = params_dic)
print (ret.url)
print (ret.text)
 
 
二、requests模块POST请求
1.post 基本用法
import  requests
data  =  {
     'hostid' '10107' ,
     'elementid' '23' ,
     'screen' '1' ,
     'name' '10.28.142.240'
}
 
ret  =  requests.post(' data = data)
print (ret.text)
 
2.post 传数据
import  requests
from  bs4  import  BeautifulSoup
url  =  'http://zabbix.kuaikuaidai.com:8888/index.php'
ret  =  requests.get(url)
 
soup  =  BeautifulSoup(ret.text,  'html.parser' )
tag  =  soup.find(attrs = { 'name' 'sid' })
sid = tag.attrs[ 'value' ]
 
tag1  =  soup.find(attrs = { 'name' 'form_refresh' })
form_refresh  =  tag1.attrs[ 'value' ]
 
 
data  =  {
     'sid' : sid,
     'form_refresh' : form_refresh,
     'name' 'admin' ,
     'password' 'kkdai123' ,
     'autologin' '1' ,
     'enter' 'Sign in'
}
 
headers  =  { 'content-type' 'text/html' }
 
zabbix_ret  =  requests.post(url,data = data, headers = headers)
 
print (zabbix_ret.status_code)
print (zabbix_ret.text)
 
 
3. 其他请求
requests.get(url, params = None * * kwargs)
requests.post(url, data = None , json = None * * kwargs)
requests.put(url, data = None * * kwargs)
requests.head(url,  * * kwargs)
requests.delete(url,  * * kwargs)
requests.patch(url, data = None * * kwargs)
requests.options(url,  * * kwargs)
 
注: 以上方法都是通过requests.request(method, url,  * * kwargs)构建
 
 
三、BeautifulSoup模块解析html文档
#获取汽车之家新闻页面
auto_home  =  requests.get(url = 'http://www.autohome.com.cn/news/' )
auto_home.encoding = 'gbk'
auto_home_news  =  auto_home.text
 
soup  =  BeautifulSoup(auto_home_news,  'html.parser' )     #返回文档结构对象
tag  =  soup.find(name = 'h3' )     #找到第一个h3标签
print (tag.name)     #打印标签名
 
1.name 标签
tag  =  soup.find(name = 'h3' )
name  =  tag.name
tag.name  =  'h4'     #设置name
 
2.attr 标签属性
print (tag.attrs)     #获取标签属性
tag.attrs[ 'id' =  '123'     #设置标签属性
 
3.children 所有子标签
tag  =  soup.find(name = 'body' )
=  tag.children
print (v)
 
4.descendants 所有子孙标签
tag  =  soup.find(name = 'body' )
=  tag.descendants
print (v)
 
5.clear 清空所有子孙标签
tag  =  soup.find(name = 'body' )
tag.clear()
print (tag)
 
6.decompose 递归删除所有标签
tag  =  soup.find(name = 'body' )
tag.decompose()
print (tag)
 
7.extract 递归删除所有标签并返回删除的标签
tag  =  soup.find(name = 'body' )
=  tag.extract()
print (v)
 
8.decode ,转换为字符串(含当前标签);decode_contents(不含当前标签)
tag  =  soup.find(name = 'body' )
=  tag.decode()
v1  =  tag.decode_contents()
print (v)
 
9.encode ,转换为字节(含当前标签);encode_contents(不含当前标签)
tag  =  soup.find(name = 'body' )
=  tag.encode()
v1  =  tag.encode_contents()
print (v)
 
10.find 获取匹配的第一个标签
tag  =  soup.find(name = 'body' )
 
11.find_all 获取匹配的所有标签
tag  =  soup.find_all(name = 'h3' )
 
12.has_attr 判断标签是否有某个属性
tag  =  soup.find(name = 'h3' )
=  tag.has_attr( 'id' )
print (v)
 
13.get_text 获取标签文本内容
tag  =  soup.find(name = 'h3' )
=  tag.get_text()
print (v)
 
14.is_empty_element ,是否是空标签(是否可以是空)或者自闭合标签
# tag = soup.find('br')
# v = tag.is_empty_element
# print(v)
 
15. 当前的关联标签
# soup.next
# soup.next_element
# soup.next_elements
# soup.next_sibling
# soup.next_siblings
  
#
# tag.previous
# tag.previous_element
# tag.previous_elements
# tag.previous_sibling
# tag.previous_siblings
  
#
# tag.parent
# tag.parents
 
16. 查找某标签的关联标签
# tag.find_next(...)
# tag.find_all_next(...)
# tag.find_next_sibling(...)
# tag.find_next_siblings(...)
  
# tag.find_previous(...)
# tag.find_all_previous(...)
# tag.find_previous_sibling(...)
# tag.find_previous_siblings(...)
  
# tag.find_parent(...)
# tag.find_parents(...)
  
# 参数同find_all
 
17.string 获取修改标签内容
tag  =  soup.find(name = 'h3' )
tag.string  =  'new content'     #设置标签内容
=  tag.string
print (v)
 
18.append 在当前标签内部追加一个标签
# tag = soup.find('body')
# tag.append(soup.find('a'))
# print(soup)
#
# from bs4.element import Tag
# obj = Tag(name='i',attrs={'id': 'it'})
# obj.string = '我是一个新来的'
# tag = soup.find('body')
# tag.append(obj)
# print(soup)
 
19.insert 在当前标签内部指定位置插入一个标签
# from bs4.element import Tag
# obj = Tag(name='i', attrs={'id': 'it'})
# obj.string = '我是一个新来的'
# tag = soup.find('body')
# tag.insert(2, obj)
# print(soup)
 
 
 
登录抽屉新热榜点赞
方法一
import  requests
i1  =  requests.get(url = 'http://dig.chouti.com/' )
i1_cookies  =  i1.cookies.get_dict()
#print(i1_cookies)
 
i2  =  requests.post(
     url = "http://dig.chouti.com/login" ,
     data = {
         'phone' '86xxxxxxxxx' ,
         'password' 'xxxxxxx' ,
         'oneMonth' : "",
     },
     cookies = i1_cookies
)
 
gpsd  =  i1_cookies[ 'gpsd' ]
i3  =  requests.post(
     url = "http://dig.chouti.com/link/vote?linksId=14723416" ,
     cookies = { 'gpsd' : gpsd}
)
 
print (i3.text)
 
 
方法二
import  requests
 
headers  =  {
     "Host" "dig.chouti.com" ,
     "Referer" "http://dig.chouti.com/" ,
     'User-Agent' 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0' ,
     'X-Requested-With' 'XMLHttpRequest'
}
session  =  requests.Session()
i1  =  session.get(url = "http://dig.chouti.com/" ,headers = headers)
i2  =  session.post(
     url = "http://dig.chouti.com/login" ,
     data = {
         'phone' "8615726697022" ,
         'password' 'xxxxxxx' ,
         'oneMonth' : ""
     },
     headers = headers
)
 
i3  =  session.post(
     url = "http://dig.chouti.com/link/vote?linksId=14723416" ,headers = headers
)
print (i3.text)
 
 
登录github账号显示项目列表
import  requests
from  bs4  import  BeautifulSoup
 
i1  =  requests.get( 'https://github.com/login' )
soup1  =  BeautifulSoup(i1.text, features = 'lxml' )
tag  =  soup1.find(name = 'input' , attrs = { 'name' 'authenticity_token' })
authenticity_token  =  tag.get( 'value' )
c1  =  i1.cookies.get_dict()
i1.close()
 
form_data  =  {
     "authenticity_token" : authenticity_token,
     'utf8' : "",
     'commit' "Sign in" ,
     'login' '758109577@qq.com' ,
     'password' 'xxxxxxxx'
}
 
i2  =  requests.post( 'https://github.com/session' , data = form_data, cookies = c1)
c2  =  i2.cookies.get_dict()
c1.update(c2)
i3  =  requests.get( 'https://github.com/settings/repositories' , cookies = c1)
 
soup3  =  BeautifulSoup(i3.text, features = 'lxml' )
list_group  =  soup3.find(name = 'div' class_ = 'listgroup' )
 
from  bs4.element  import  Tag
for  child  in  list_group.children:
     if  isinstance (child, Tag):
         project_tag  =  child.find(name = 'a' class_ = 'mr-1' )
         size_tag  =  child.find(name = 'small' )
         temp  =  "项目: %s(%s); 项目路径: %s"  %  (project_tag.get( 'href' ), size_tag.string, project_tag.string, )
         print (temp)
         
方法二
import  requests
from  bs4  import  BeautifulSoup
 
session  =  requests.Session()
i1  =  session.get( 'https://github.com/login' )
soup1  =  BeautifulSoup(i1.text,features = 'lxml' )
tag  =  soup1.find(name = 'input' , attrs = { 'name' 'authenticity_token' })
authenticity_token  =  tag.get( 'value' )
c1  =  i1.cookies.get_dict()
i1.close()
 
form_data  =  {
     "authenticity_token" : authenticity_token,
     'utf8' : "",
     'commit' "Sign in" ,
     'login' '758109577@qq.com' ,
     'password' 'xxxxxxxxxx'
}
 
i2  =  session.post( 'https://github.com/session' , data = form_data)
c2  =  i2.cookies.get_dict()
c1.update(c2)
i3  =  session.get( 'https://github.com/settings/repositories' )
 
soup3  =  BeautifulSoup(i3.text, features = 'lxml' )
list_group  =  soup3.find(name = 'div' class_ = 'listgroup' )
 
from  bs4.element  import  Tag
 
for  child  in  list_group.children:
     if  isinstance (child, Tag):
         project_tag  =  child.find(name = 'a' class_ = 'mr-1' )
         size_tag  =  child.find(name = 'small' )
         temp  =  "项目: %s(%s); 项目路径: %s"  %  (project_tag.get( 'href' ), size_tag.string, project_tag.string, )
         print (temp)
         
         
知乎登录
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import  time
 
import  requests
from  bs4  import  BeautifulSoup
 
session  =  requests.Session()
 
i1  =  session.get(
     url = 'https://www.zhihu.com/#signin' ,
     headers = {
         'User-Agent' 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36' ,
     }
)
 
soup1  =  BeautifulSoup(i1.text,  'lxml' )
xsrf_tag  =  soup1.find(name = 'input' , attrs = { 'name' '_xsrf' })
xsrf  =  xsrf_tag.get( 'value' )
 
current_time  =  time.time()
i2  =  session.get(
     url = 'https://www.zhihu.com/captcha.gif' ,
     params = { 'r' : current_time,  'type' 'login' },
     headers = {
         'User-Agent' 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36' ,
     })
 
with  open ( 'zhihu.gif' 'wb' ) as f:
     f.write(i2.content)
 
captcha  =  input ( '请打开zhihu.gif文件,查看并输入验证码:' )
form_data  =  {
     "_xsrf" : xsrf,
     'password' 'xxxxxx' ,
     "captcha" : captcha,
     'email' '7xxxxx@qq.com'
}
i3  =  session.post(
     url = 'https://www.zhihu.com/login/email' ,
     data = form_data,
     headers = {
         'User-Agent' 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36' ,
     }
)
 
i4  =  session.get(
     url = 'https://www.zhihu.com/settings/profile' ,
     headers = {
         'User-Agent' 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36' ,
     }
)
 
soup4  =  BeautifulSoup(i4.text,  'lxml' )
tag  =  soup4.find( id = 'rename-section' )
nick_name  =  tag.find( 'span' , class_ = 'name' ).string
print (nick_name)







     本文转自小白的希望 51CTO博客,原文链接:http://blog.51cto.com/haoyonghui/1972754 ,如需转载请自行联系原作者



相关文章
|
7月前
|
数据采集 数据安全/隐私保护 索引
爬虫学习
爬虫学习
|
7月前
|
数据采集 JSON 网络协议
爬虫学习(前期知识学习)
爬虫学习(前期知识学习)
|
数据采集 数据可视化 Python
Python爬虫学习——简单爬虫+可视化
Python爬虫学习——简单爬虫+可视化
178 0
|
数据采集 Web App开发 存储
Python爬虫学习:Cookie 和 Session 的区别是什么?
Cookie意为“甜饼”,是由W3C组织提出,最早由Netscape社区发展的一种机制。目前Cookie已经成为标准,所有的主流浏览器如IE、Netscape、Firefox、Opera等都支持Cookie。
157 0
|
数据采集 XML 前端开发
爬虫学习:pyquery的使用
pyquery:一个类似jquery的python库,pyquery 允许我们对 xml 文档进行 jquery 查询。API 尽可能地类似于 jquery。pyquery 使用 lxml 进行快速 xml 和 html 操作,熟悉jquery的朋友快来学习python中的pyquery。
131 0
爬虫学习:pyquery的使用
|
数据采集 XML 移动开发
爬虫学习:Beautiful Soup的使用
一个强大的解析工具——Beautiful Soup,只需简单的几段代码就可以完成网页中某个信息的提取,一起来见识一下它的强大之处叭!
134 0
爬虫学习:Beautiful Soup的使用
|
数据采集 编译器 开发者
爬虫学习:XPath的使用
熟知XPath的使用来进行HTML信息锁定并获取
218 0
爬虫学习:XPath的使用
|
数据采集 自然语言处理 测试技术
爬虫学习:正则表达式
爬虫学习需要熟知正则表达式的正确使用
125 0
爬虫学习:正则表达式
|
数据采集 XML 存储
Python爬虫在学习中整理的一些小技巧
如果你对工作不满意或对生活不满意,请不用抱怨,因为毫无用处,你只有努力提升技能,努力的完善自己,不断地向前奔跑,才会越来越好的!