鱼C论坛

 找回密码
 立即注册
查看: 2819|回复: 1

[技术交流] POST中如何使用Requests Payload的表单提交

[复制链接]
发表于 2019-9-13 17:29:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
偶尔无意看到一个鱼友小伙伴,发的求助帖,自己看了也算蛮有意思的,这里做个记录


目标站:http://gs.amac.org.cn/amac-infod ... er/managerList.html,在翻页的时候,就可以抓到数据请求的端口啦,不过初看可能觉得有些奇怪
一般向后台拿数据,大多都是GET请求,不过这里居然是用POST,不过“客随主便”啦
request.JPG
如上图,查询字段page,和szie,还有关于rand这个是一个随机数而已,虽然隔久了再次请求的时候rank不一样,可以复用的
rank.JPG

好啦,这里稍微提示下,这个POST的表达提交方式,和平时的Form data不一样哈
res = requests.post(url=url, data=json.dumps({}), headers=headers)

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-9-14 21:21:43 | 显示全部楼层
本帖最后由 huazisheng 于 2019-9-15 06:31 编辑
import requests
import json
from lxml import etree
import time
import csv
fp=open('C:/Users/Administrator/Desktop/simu.csv','wt',newline='',encoding='utf-8')
writer=csv.writer(fp)
writer.writerow(('网址','企业名称','负责人','成立时间','注册地址','办公地址','实缴资本','机构类型','全职人数','资格人数','机构网址'))
headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Length': '2',
    'Content-Type': 'application/json',
    'Host': 'gs.amac.org.cn',
    'Origin': 'http://gs.amac.org.cn',
    'Pragma': 'no-cache',
    'Referer': 'http://gs.amac.org.cn/amac-infodisc/res/pof/manager/managerList.html',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
    'X-Requested-With': 'XMLHttpRequest'
}
def get_info(url):
        res=requests.get(url,headers=headers)
        res.raise_for_status()
        res.encoding=res.apparent_encoding
        seletctor=etree.HTML(res.text)
        企业名称=seletctor.xpath('//*[@id="complaint1"]')[0].text[:-6]
        成立时间=seletctor.xpath('/html/body/div/div[2]/div/table/tbody/tr[7]/td[4]')[0].text
        注册地址=seletctor.xpath('/html/body/div/div[2]/div/table/tbody/tr[8]/td[2]')[0].text
        办公地址=seletctor.xpath('/html/body/div/div[2]/div/table/tbody/tr[9]/td[2]')[0].text
        实缴资本=seletctor.xpath('/html/body/div/div[2]/div/table/tbody/tr[10]/td[4]')[0].text
        机构类型=seletctor.xpath('/html/body/div/div[2]/div/table/tbody/tr[12]/td[2]')[0].text
        全职人数=seletctor.xpath('/html/body/div/div[2]/div/table/tbody/tr[13]/td[2]')[0].text
        资格人数=seletctor.xpath('/html/body/div/div[2]/div/table/tbody/tr[13]/td[4]')[0].text
        负责人=seletctor.xpath('/html/body/div/div[2]/div/table/tbody/tr[22]/td[2]')[0].text
        #/html/body/div/div[2]/div/table/tbody/tr[22]/td[2]                        #有的二级子页面是这个提取地址
        #/html/body/div/div[2]/div/table/tbody/tr[21]/td[2]                        #有的二级子页面是这个提取地址,不完全一致
        机构网址=seletctor.xpath('/html/body/div/div[2]/div/table/tbody/tr[14]/td[2]/a')[0].text
        #/html/body/div/div[2]/div/table/tbody/tr[14]/td[2]                        #空白时是这个提取地址
        #/html/body/div/div[2]/div/table/tbody/tr[14]/td[2]/a                #非空白时是这个提取地址,不完全一致
        writer.writerow((url,企业名称,负责人,成立时间,注册地址,办公地址,实缴资本,机构类型,全职人数,资格人数))
for i in range(0,3):
        rank = 0.3389959824021722
        url = f"http://gs.amac.org.cn/amac-infodisc/api/pof/manager?rand=0.6122678870895568&page={i}&size=20"
        res = requests.post(url=url, data=json.dumps({}), headers=headers).json().get("content")  # res是一个字典
        for each in res:
                url = "http://gs.amac.org.cn/amac-infodisc/res/pof/manager/" + each.get("url")   # 构建二级子页面请求,需要采集其他数据,也可以采用。get("key")的方式
                get_info(url)
                #time.sleep(2)
fp.close()

Xpath方式提取二级子页面时,部分提取地址不完全一致,导致信息提取失败或提取错误

截图.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-17 03:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表