python - 如何在 python 中使用 mechanize 发送带有 post 请求的原始 JSON 数据

标签 python web-scraping mechanize mechanize-python

我正在尝试使用 python 脚本将 JSON 数据发布到 api。我正在使用 python 2 和 Mechanize 包裹。

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() 发送数据时然后数据正在发送但未在服务器中处理,因为服务器期望通过 post 请求获得 JSON 数据。请有人帮忙解决这个问题。

最佳答案

好的,所以决定按照@Manhattan 的建议使用 requests 模块。使用请求发送带有 json 数据的 post 请求要容易得多。

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

这里的 json 参数只是一个 python 字典。和 headers 也是一本字典。
Official documentation

关于python - 如何在 python 中使用 mechanize 发送带有 post 请求的原始 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50914432/

相关文章:

python 脚本转储 ELF(核心和输出)?

python - vscode和docker已在使用(Errno 98)django的地址

python - BS 无法在 Selenium 检索页面中获取部分 id

ruby - 我们可以一起使用 Mechanize 和 Selenium WebDriver 吗? ruby

Ruby Mechanize https 错误

python - 只允许访问 django 中的未登录用户

python - 使用自动换行创建可调整大小/多行 Tkinter/ttk 标签

html - QuerySelector 中的单引号

html - 通过 Jsoup 获取 innerHTML

ruby-on-rails - Ruby Mechanize.get(url) 的两种不同语法 - 有什么好处?