码迷,mamicode.com
首页 > 编程语言 > 详细

【Python requests多页面爬取案例】 񩲝

时间:2019-08-18 16:04:23      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:erp   imp   business   案例   list   any   cti   portal   rop   

原文: http://blog.gqylpy.com/gqy/321

"```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()
```

"

原文: http://blog.gqylpy.com/gqy/321

【Python requests多页面爬取案例】 񩲝

标签:erp   imp   business   案例   list   any   cti   portal   rop   

原文地址:https://www.cnblogs.com/gqy02/p/11372417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!