第一步:requests

get请求

# -*- coding:utf-8  -*-
# 日期:2018/5/15 17:46
# Author:小鼠标
import requests
url = "http://www.baidu.com"
#res = requests.get(url)  #方法1 res = requests.request('get',url) #方法2
print('响应状态码:',res.status_code) print('响应内容:',res.text)

post请求

# -*- coding:utf-8  -*-
# 日期:2018/5/15 17:46
# Author:小鼠标
import requests
url = "http://www.baidu.com"
data = {
'username': 'xiaoshubiao',
'pwd': 'xiaoshubiao'
}
res = requests.post(url,data)
print('响应状态码:',res.status_code)
print('响应内容:',res.text)

第二步:伪装浏览器和伪造cookie

# -*- coding:utf-8  -*-
# 日期:2018/5/15 17:46
# Author:小鼠标
import requests
url = "http://www.baidu.com"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36'
' (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.39'
'64.2 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0'
'.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive'
}
cookies = dict(name='xiaoshubiao')
res = requests.get(url,headers = headers,cookies = cookies)
print('响应状态码:',res.status_code)
print('响应内容:',res.text)

第三步:使用代理ip

# -*- coding:utf-8  -*-
# 日期:2018/5/15 17:46
# Author:小鼠标
import requests
url = "http://www.baidu.com"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36'
' (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.39'
'64.2 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0'
'.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive'
}
cookies = dict(name='xiaoshubiao')
proxies = {'http':'218.73.134.234:36602'}
res = requests.get(url,headers = headers,cookies = cookies,proxies = proxies)
print('响应状态码:',res.status_code)
print('响应内容:',res.text)

第四步:内容解析

# -*- coding:utf-8  -*-
# 日期:2018/5/15 17:46
# Author:小鼠标
import requests
from bs4 import BeautifulSoup
url = "http://news.sina.com.cn/guide/"
res = requests.get(url)
res.encoding = 'utf-8'
web_data = res.text
#内容解析
soup = BeautifulSoup(web_data,'lxml')
title_list = soup.select('title') #获取标签内容 返回为列表
a_list = soup.select('a')
ul_list = soup.select('ul.list01') #获取类名为list01的ul的内容 返回为列表
div_list = soup.select('div#tab01') #获取id为tab01的内容 返回为列表
for title , a in zip(title_list,a_list):
title_content = title.get_text() #获取标签内容的值
a_href = a.get('href') #获取标签的属性的值
print(title_content,a_href)

 

学习笔记 requests + BeautifulSoup的更多相关文章

  1. Python爬虫学习三------requests+BeautifulSoup爬取简单网页

    第一次第一次用MarkDown来写博客,先试试效果吧! 昨天2018俄罗斯世界杯拉开了大幕,作为一个伪球迷,当然也得为世界杯做出一点贡献啦. 于是今天就编写了一个爬虫程序将腾讯新闻下世界杯专题的相关新 ...

  2. 吴裕雄--python学习笔记:BeautifulSoup模块

    import re import requests from bs4 import BeautifulSoup req_obj = requests.get('https://www.baidu.co ...

  3. 吴裕雄--天生自然python学习笔记:beautifulsoup库的使用

    Beautiful Soup 库简介 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能.它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简 ...

  4. python学习笔记(26)-request模块

    python学习笔记 #requests import requests #from class_005.http_resuest import HttpRequest login_url = &qu ...

  5. Requests:Python HTTP Module学习笔记(一)(转)

    Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...

  6. python网络爬虫学习笔记(二)BeautifulSoup库

    Beautiful Soup库也称为beautiful4库.bs4库,它可用于解析HTML/XML,并将所有文件.字符串转换为'utf-8'编码.HTML/XML文档是与“标签树一一对应的.具体地说, ...

  7. 孤荷凌寒自学python第七十天学习并实践beautifulsoup对象用法3

    孤荷凌寒自学python第七十天学习并实践beautifulsoup对象用法3 (完整学习过程屏幕记录视频地址在文末) 今天继续学习beautifulsoup对象的属性与方法等内容. 一.今天进一步了 ...

  8. 孤荷凌寒自学python第六十九天学习并实践beautifulsoup对象用法2

    孤荷凌寒自学python第六十九天学习并实践beautifulsoup对象用法2 (完整学习过程屏幕记录视频地址在文末) 今天继续学习beautifulsoup对象的属性与方法等内容. 一.今天进一步 ...

  9. 孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1

    孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1 (完整学习过程屏幕记录视频地址在文末) 感觉用requests获取到网页的html源代码后,更重要的工作其实是分析得到的内 ...

随机推荐

  1. ansible批量管理工具的搭建与简单的操作

    ansible的安装 # [root@localhost ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@localhost ...

  2. RAID的详细配置

    一.RAID 1.RAID机制通过使用多硬盘并行工作的方式来提高硬盘的IO性能 2.RAID分为多种,称之为RAID level,RAID共有7级:RAID0~RAID6 3.常用的RAID级别有:R ...

  3. socket练习--ssh

    服务器端: # -*-coding:utf-8-*- # Author:sunhao import socket import os server = socket.socket() ip_port ...

  4. red hat防火墙的开启与关闭及状态查看方法

    Redhat使用了SELinux来增强安全, 首先怎么查看防火墙的状态呢? a.可以通过如下命令查看iptables防火墙状态: chkconfig --list iptables b. selinu ...

  5. linux之文件增删改查

  6. ChinaCock界面控件介绍-TCCBarcodeCreator

    条码生成器,可以生成各种条码,包括二维码.这是一个不可视控件.用起来依旧简单. 属性说明: BarCodeColor:生成条码的颜色 BarcodeFormat:生成条码的类型,支持的条码类型: Bo ...

  7. disjoint set

    MAKE-SET.x/ creates a new set whose only member (and thus representative) is x. Since the sets are d ...

  8. 剑指Offer 42. 和为S的两个数字 (其他)

    题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. 题目 ...

  9. [转]腾讯研发类笔试面试试题(C++方向)

     https://blog.csdn.net/Xiongchao99/article/details/73381280 1.C和C++的特点与区别? 答:(1)C语言特点:1.作为一种面向过程的结构化 ...

  10. Python2和Python3安装注意事项

    1. 到官网 https://www.python.org/downloads/windows/ 下载 Windows x86-64 executable installer版本: 2. python ...