【Python requests多页面爬取案例】
"```python
import requests
from fake_useragent import UserAgent # 随机ua库
class Boring():
def __init__(self, page_scope=(4, 7)):
"""
:param page_scope: 页码范围
"""
self.page_scope = page_scope
self.all_id = self.get_all_company_id()
self.enterprise_info = self.get_all_company_info()
self.show_enterprise_info()
@property
def firefox_ua(self):
"""返回随机火狐UA头"""
ua = UserAgent(use_cache_server=False)
return {'User-Agent': ua.Firefox} # ua.Firefox:随机生成火狐浏览器UA
def get_all_company_id(self):
"""
将返回指定页码数内的公司的id
:param start_page: 起始页码
:param end_page: 结束页码
"""
all_id = {}
url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList' # 此连接见图1
for page in range(self.page_scope[0], self.page_scope[1] + 1):
json_text = requests.post(url, data=self.post_data(page), headers=self.firefox_ua).json()
current_page_all_id = [dict['ID'] for dict in json_text['list']]
all_id.setdefault(page, current_page_all_id)
return all_id
def get_all_company_info(self):
"""开始获取公司信息"""
url = 'http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById' # 见图3
enterprise_info = {}
for page in self.all_id:
for id in self.all_id.get(page):
response = requests.post(url, data={'id': id}, headers=self.firefox_ua) # data={'id': id}:见图4
if response.headers['Content-Type'] == 'application/json;charset=UTF-8':
json_text = response.json()
enterprise_info.setdefault(json_text.get('businessPerson'), json_text.get('epsName'))
# 这里仅获取企业负责人和企业名
return enterprise_info
def show_enterprise_info(self):
[print(k, v) for k, v in self.enterprise_info.items()]
def post_data(self, page):
"""获取公司列表时要提交的form"""
return {
'on': 'true',
'page': page,
'pageSize': '15',
'productName': '',
'conditionType': '1',
'applyname': '',
'applysn': '',
} # 见图2
go
Boring()
"
【Python requests多页面爬取案例】的更多相关文章
- python requests库网页爬取小实例:亚马逊商品页面的爬取
由于直接通过requests.get()方法去爬取网页,它的头部信息的user-agent显示的是python-requests/2.21.0,所以亚马逊网站可能会拒绝访问.所以我们要更改访问的头部信 ...
- python requests库网页爬取小实例:百度/360搜索关键词提交
百度/360搜索关键词提交全代码: #百度/360搜索关键词提交import requestskeyword='Python'try: #百度关键字 # kv={'wd':keyword} #360关 ...
- Python Requests库网络爬取全代码
#爬取京东商品全代码 import requestsurl = "http://item.jd.com/2967929.html"try: r = requests.get(url ...
- python Requests库网络爬取IP地址归属地的自动查询
#IP地址查询全代码import requestsurl = "http://m.ip138.com/ip.asp?ip="try: r = requests.get(url + ...
- Python Requests库入门——应用实例-京东商品页面爬取+模拟浏览器爬取信息
京东商品页面爬取 选择了一款荣耀手机的页面(给华为打广告了,荣耀play真心不错) import requests url = "https://item.jd.com/7479912.ht ...
- 使用requests简单的页面爬取
首先安装requests库和准备User Agent 安装requests直接使用pip安装即可 pip install requests 准备User Agent,直接在百度搜索"UA查询 ...
- Python使用urllib,urllib3,requests库+beautifulsoup爬取网页
Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...
- python爬爬爬之单网页html页面爬取
python爬爬爬之单网页html页面爬取 作者:vpoet mail:vpoet_sir@163.com 注:随意copy 不用告诉我 #coding:utf-8 import urllib2 Re ...
- Python爬虫实例:爬取B站《工作细胞》短评——异步加载信息的爬取
很多网页的信息都是通过异步加载的,本文就举例讨论下此类网页的抓取. <工作细胞>最近比较火,bilibili 上目前的短评已经有17000多条. 先看分析下页面 右边 li 标签中的就是短 ...
随机推荐
- Spring的BeanFactory和FactoryBean
官方定义 BeanFactory:Spring Bean容器的根接口 FactoryBean:各个对象的工厂接口,如果bean实现了这个接口,它将被用作对象的工厂,而不是直接作为bean实例. 源码解 ...
- 数据结构(集合)学习之List
集合 框架关系图: Collection接口下面有三个子接口:List.Set.Queue.此篇是关于List<E>的简单学习总结. 补充:HashTable父类是Dictionary,不 ...
- 当课堂因监控技术变“囚笼”,存在争议的AI使用场景该被抵制吗?
当马云和马斯克高谈阔论AI是否会影响人类社会时,尚无"感情"的AI已在校园中"作恶".近日,一张AI监控课堂的GIF在网上迅速刷屏.这张GIF中记录了课堂中所有 ...
- Windows下Anaconda安装、换源与更新
Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.当你尝试pip install xxx时出现各种意外和依赖问题,那么conda就是一 ...
- Qt Gui 第七章
1.processEvents 该函数用来处理窗口的各种事件,重绘和event ; row < RowCount; ++row) { ; column < ColumnCount; ++c ...
- Beego模板 循环和判断几个例子
Beego模板 循环和判断几个例子 Beego的前端几乎是另一种语言.一些循环.判断,不细看文档真的做不出来. 0. Beego的View模板语法规则: beego前端(view)统一使用了 {{ 和 ...
- selenium统计网页加载时间
参考网址: https://blog.csdn.net/thlzjfefe/article/details/99712974 https://blog.csdn.net/weixin_43664254 ...
- windows环境下安装JDK
一.环境准备 Windows10 jdk-9.0.1 二.下载并安装JDK 到Java的官网下载JDK安装包,地址:http://www.oracle.com/technetwork/java/jav ...
- bootstrap-datatimepicker插件的使用
1.引入相关的css.js文件(因为是bootstrap的,首先应该引入bootstrap.css和bootstrap.js): <link rel="stylesheet" ...
- Selenium3+python自动化011-unittest生成测试报告(HTMLTestRunner)
批量执行完用例后,生成的测试报告是文本形式的,不够直观,为了更好的展示测试报告,最好是生成HTML格式的. unittest里面是不能生成html格式报告的,需要导入一个第三方的模块:HTMLTest ...