簡體   English   中英

如何在python中使用機械化發送帶有后期請求的原始JSON數據

[英]How to send raw JSON data with a post request using mechanize in python

我正在嘗試使用python腳本將JSON數據發布到api。 我正在使用python 2和機械化包。

import mechanize
import json, logging, sys, urllib

br = mechanize.Browser()
url = 'url_here'
headers = [('User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0'),
('Content-Type', 'application/json')]
br.set_handle_robots(False)
br.set_debug_responses(True)
br.set_debug_redirects(True)
br.set_debug_http(True)
logger = logging.getLogger("mechanize")
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)
body = {"audience":{"name":"asd","criteria":[{"category":"common","label":"Industry","tags":[{"tag":"Information Technology","text":"Information Technology","score":2,"type":"Similar"},{"tag":"Computer Software","text":"Computer Software","score":2,"type":"Similar"},{"tag":"Marketing and Advertising","text":"Marketing and Advertising","score":2,"type":"Similar"}],"type":"input-group","icon":"restaurant_menu","operation":"include","suggestions":True,"related":False,"multiple":True,"rankings":True,"showStrictness":True,"strict":False,"stub":{"tags":[],"values":[]},"values":["Information Technology","Computer Software","Marketing and Advertising"],"field":"industry","scope":"companies"},{"category":"common","label":"Contact Location","type":"location-group","icon":"person_pin_circle","operation":"include","multiple":True,"suggestions":True,"related":False,"tip":"Find people based on their location. You can filter by Country, and Region / City (for example, San Francisco Bay Area).","options":[{"value":"country","label":"Country","placeholder":"e.g... United States, Australia"},{"value":"locality","label":"Region / City","placeholder":"e.g... San Francisco, Toronto"}],"stub":{"tags":[],"values":[]},"tags":[],"values":["India","United States of America","United Kingdom"],"field":"country","childField":"country","scope":"contacts"},{"category":"tech","label":"Technology Used","type":"input-group","operation":"include","suggestions":True,"related":False,"icon":"power","multiple":True,"rankings":True,"placeholder":"e.g... Salesforce, jQuery, Ruby","tip":"Find companies based on specific web-based technologies they use. Note: This filter will narrow your search significantly. Any companies that dont use the technology you enter will be excluded from the results. ","stub":{"tags":[],"values":[]},"tags":[{"tag":"jQuery","text":"jQuery","score":2,"type":"Similar"},{"tag":"Angular JS v2","text":"Angular JS v2","score":0,"type":"Similar"}],"values":["jQuery","Angular JS v2"],"field":"technology","scope":"companies"}],"purchasedLeads":0,"estimatedReach":0,"maxLeadsPerCompany":3,"searchLeadCount":500,"emailGuesserScope":"professional"}}
br.addheaders = headers
response = br.open(url, data=json.dumps(body))
print response

但是我無法成功發送JSON數據。 但是,當我使用urllib.urlencode()發送數據時,正在發送數據但未在服務器中對其進行處理,因為服務器希望通過發布請求獲得JSON數據。 請有人幫忙。

好的,因此決定使用@Manhattan建議的請求模塊。 使用請求發送帶有JSON數據的發布請求更加容易和簡單。

r = requests.post(url, json={"audienceId":id,"leadCount":500}, headers=h)

這里的json參數只是一個python字典。 標頭也是字典。 官方文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM