【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 标签中的就是短 ...
随机推荐
- Mac下maven安装
1.下载路径:https://maven.apache.org/download.cgi 要想查看历史版本:则点击archives. 点击binaries 就可以下载对应的maven. Binary ...
- 关于所学,及JNI问题
上周每天学习Java两个小时,随后两个小时里对教材上的例子进行验证,学会了如何使用Javac对文件进行终端编译,输出,但由于所下载的 jdk版本问题出现了JNI问题,正在尝试解决.并学会了如何使用ec ...
- Chrome 插件 postman 可以在线post
地址:https://chrome.google.com/webstore/detail/fhbjgbiflinjbdggehcddcbncdddomop
- C#MVC用ZXing.Net生成二维码/条形码
开篇:zxing.net是.net平台下编解条形码和二维码的工具. 首先创建新项目 选择MVC模板 添加一个控制器 在项目引用中的引用ZXing 进行联网下载 控制器需要引用 后台控制器 pu ...
- JAVA8List排序,(升序,倒序)
List<Integer> integerList = Arrays.asList(4, 5, 2, 3, 7, 9); List<Integer> collect = int ...
- 虚拟机NAT模式连接外网
虚拟机三种联网方式: 一.NAT(推荐使用) 功能:①可以和外部网络连通 ②可以隔离外部网络 二.桥接模式 功能:直接 ...
- Wannafly Camp 2020 Day 2D 卡拉巴什的字符串 - 后缀自动机
动态维护任意两个后缀的lcp集合的mex,支持在串末尾追加字符. Solution 考虑在 SAM 上求两个后缀的 LCP 的过程,无非就是找它们在 fail 树上的 LCA,那么 LCP 长度就是这 ...
- 关于IO板的输出(Do的写入)
IO板的输入输出遵循MODBUS协议 1.单个DO开关量寄存器写入-功能码05 例子-打开信道3 01 05 00 02 00 00 CD CA 01 从机地址(由io的配置文件决定) 05 功能码 ...
- Django生成脚本迁移文件时,报错django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
一.本人环境:django:3.0.2, python:3.8.1, pymysql:0.9.3 二.解决步骤: 1.django目录下找到 base.py文件: 2.在base.py文件中注释以下 ...
- .NetCore学习笔记:三、基于AspectCore的AOP事务管理
AOP(面向切面编程),通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是函数式编程的一种衍生范型.利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑 ...