【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 标签中的就是短 ...
随机推荐
- java-局部变量,成员变量区别
1. 内存中的位置 成员变量: 堆内存 局部变量: 栈内存 2. 生命周期 成员变量:随着对象的创建而存在,随着对象的消失而消失 局部变量:随着方法的调用而存在,随着方法调用完毕而消失 3. 注意事项 ...
- VMware Workstation Pro工具
安装包 链接:https://pan.baidu.com/s/1n-URb83lHtric3Ds8UbF9Q 提取码:c9z5 密钥 FF31K-AHZD1-H8ETZ-8WWEZ-WUUVA CV7 ...
- Mybatis的增删改和log4j的基础配置
带条件查询 mapper文件的内容: <select id="getSelectElectron" resultType="electron"> s ...
- (int)、int.Parse()、int.TryParse()、Convert.ToInt32()区别
请看代码: //1.null. //int i1 = (int)null;//编译时报错:无法将“null”转换为“int”,因为后者是不可以为“null”的值类型. //int i2 = int.P ...
- python之三目运算符的替代品?
# 不知曾几何时,你是否也觉得Python的三目运算写起来很麻烦呢?(没有过) # 比如: a, b = 3, 4 c = a if a > b else b d = a if a < b ...
- ssrf漏洞利用(内网探测、打redis)
摘要:存在ssrf漏洞的站点主要利用四个协议,分别是http.file.gopher.dict协议. file协议拿来进行本地文件的读取,http协议拿来进行内网的ip扫描.端口探测,如果探测到637 ...
- 1.(group by)如何让group by分组后,每组中的所有数据都显示出来
问题描述:表如下,如何让这个表按device_id这个字段分组,且组中的每条数据都查寻出来?(假如说这个表名为:devicedata) 错误答案:select * from devicedata GR ...
- javaScript中的异步编程模式
1.事件模型 let button = document.getElementById("my-btn"); button.onclick = function(event) { ...
- codeforces每日一题1-10
目录: 1.1093D. Beautiful Graph(DFS染色) 2.514C - Watto and Mechanism(Tire) 3.69E.Subsegments(STL) 4.25C. ...
- 查询避免Unknown column ‘xxx’ in ‘where clause
问题: 单从字面理解,我们很容易得出列名称不存在的结论, 但是,很多时候并不是列名出错造成的,而是由于拼凑sql语句时对字符类型数据没有用引号引起来造成的. 例子: 例如: String sql=& ...