使用requests库制作Python爬虫

yipeiwu_com6年前Python爬虫

使用python爬虫其实就是方便,它会有各种工具类供你来使用,很方便。Java不可以吗?也可以,使用httpclient工具、还有一个大神写的webmagic框架,这些都可以实现爬虫,只不过python集成工具库,使用几行爬取,而Java需要写更多的行来实现,但目的都是一样。

下面介绍requests库简单使用:

#!/usr/local/env python
# coding:utf-8

import requests

#下面开始介绍requests的使用,环境语言是python3,使用下面的网址作为参考
#http://www.sse.com.cn/market/bonddata/data/tb/

request_param = {'jsonCallBack': 'jsonpCallback6588',
   'isPagination': 'true',
   'sqlId': 'COMMON_BOND_XXPL_ZQXX_L',
   'BONDTYPE': '地×××府债券',
   'pageHelp.pageSize': '25',
   'pageHelp.pageNo': '2',
   'pageHelp.beginPage': '2',
   'pageHelp.cacheSize': '1',
   'pageHelp.endPage': '21'}

user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
referer = 'http://www.sse.com.cn/market/bonddata/data/ltb/'
#设置headers
headers = {'User-Agent': user_agent, 'Referer': referer}
#设置代理
proxy = {
 "http":"http://113.214.13.1:8000"
}

# 需要请求的URL地址
request_url = 'http://query.sse.com.cn/commonQuery.do?'

#设置请求地址
response = requests.get(request_url, headers=headers, proxies=proxy, params=request_param);
print(response.status_code)
#文本响应内容
print(response.text)
#json格式响应内容
print(response.json())
#二进制响应内容
print(response.content)
#原始格式
print(response.raw)

相关文章

Python爬虫实战之12306抢票开源

Python爬虫实战之12306抢票开源

今天就和大家一起来讨论一下python实现12306余票查询(pycharm+python3.7),一起来感受一下python爬虫的简单实践 我们说先在浏览器中打开开发者工具(F12),...

python爬取酷狗音乐排行榜

本文为大家分享了python爬取酷狗音乐排行榜的具体代码,供大家参考,具体内容如下 #coding=utf-8 from pymongo import MongoClient im...

python3爬虫学习之数据存储txt的案例详解

python3爬虫学习之数据存储txt的案例详解

上一篇实战爬取知乎热门话题的实战,并且保存为本地的txt文本 先上代码,有很多细节和坑需要规避,弄了两个半小时 import requests import re headers...

python网络爬虫 CrawlSpider使用详解

CrawlSpider 作用:用于进行全站数据爬取 CrawlSpider就是Spider的一个子类 如何新建一个基于CrawlSpider的爬虫文件 sc...

Python实现爬虫从网络上下载文档的实例代码

最近在学习Python,自然接触到了爬虫,写了一个小型爬虫软件,从初始Url解析网页,使用正则获取待爬取链接,使用beautifulsoup解析获取文本,使用自己写的输出器可以将文本输出...