如何让更改后的网址发布数据?

时间:2017-12-11 11:58:01

标签: python python-requests

我想从[网站] [1]获取json数据。现在我得到了作业列表p_ordNo。我想了解这份工作的细节。我发现它使用post methold.So,我使用这样的请求发布数据:

url = "https://www2.jobs.gov.hk/1/0/WebServices/QuickviewWS.asmx/F_GetJobCardDetail"
postdata = {'p_ordNo': card,'p_langOpt': '3','p_liveOnly': ''}
scode = requests.post(url, data = postdata,timeout=30).status

它像这样返回代码502

[![在此处输入图像说明] [2]] [2]

[![在此处输入图像说明] [3]] [3]

我发现posturl已更改,但我不知道如何获取更改后的网址。

2 个答案:

答案 0 :(得分:1)

我不确定所有这些步骤是否必要,但这就是我如何使用它,请阅读代码中的注释:

import requests
from bs4 import BeautifulSoup
import json

# use requests.session() to capture cookies.
session = requests.session()

# Set some headers it needs at least some of these.
headers={'User-Agent':"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv'\:'57.0) Gecko/20100101 Firefox/57.0",
'Accept': 'application/json, text/javascript, */*','Accept-Language': 'en-GB,en;q=0.8,fr;q=0.6,es;q=0.4,pl;q=0.2',
'Accept-Encoding': 'gzip, deflate',
'Referer':'http://www1.jobs.gov.hk/1/0/WebForm/jobseeker/jobsearch/quickview.aspx?ResetTimeStamp=true&SearchFor=jobtype&id=1',
'Content-Type':' application/json; charset=utf-8',
'X-Requested-With':'XMLHttpRequest',
'Connection':' keep-alive'}

# Make a GET request to get the value we use for p_ordNo later.
url = "http://www2.jobs.gov.hk/1/0/WebForm/jobseeker/jobsearch/quickview.aspx?ResetTimeStamp=true&SearchFor=jobtype&id=1"
page = session.get(url, headers=headers).text

# Read the value we use for p_ordNo later.
soup = BeautifulSoup(page, "lxml")
value = soup.find("input", {"name": "ctl00$ContentPlaceHolder1$uxSelectedOrdNo"})["value"]

# Make the POST request it is not JSON as it is not quoted correctly.
url = "http://www2.jobs.gov.hk/1/0/WebServices/Quickview3WS.asmx/F_GetJobCardDetail"

# get the response as a JSON object
result = session.post(url, headers=headers, data = "{{p_ordNo:'{}',p_langOpt: '3',p_liveOnly: ''}}".format(value),timeout=30).json()

#Print the result.
print (json.dumps(result, indent=4, sort_keys=True, ensure_ascii=False))

答案 1 :(得分:0)

我修改了请求,如下所示:

workCollector := make([]*[]*Node, 5)
for i ; i < 5; i++ {
    go.Process(i)
}

func Process(threadNum int){
    localList := workCollector[threadNum]
    *localList = append(*localList, NewNode(UUID.()))
}

现在它正在运作

postdata = {'p_ordNo': card,
            'p_langOpt': '3',
            'p_liveOnly': ''}

session.post(url, data = json.dumps(postdata),headers=headers).content