GVKun编程网logo

Python:尝试使用请求发布表单(python 发请求)

10

在本文中,您将会了解到关于Python:尝试使用请求发布表单的新资讯,同时我们还将为您解释python发请求的相关在本文中,我们将带你探索Python:尝试使用请求发布表单的奥秘,分析python发请

在本文中,您将会了解到关于Python:尝试使用请求发布表单的新资讯,同时我们还将为您解释python 发请求的相关在本文中,我们将带你探索Python:尝试使用请求发布表单的奥秘,分析python 发请求的特点,并给出一些关于python – Pylons:尝试服务时已经使用的地址、Python – 尝试使用意外的mimetype解码JSON:、Python 连接在第一次尝试使用导入请求时被拒绝、python-2.7 – 无法使用python和请求发布到solr服务器的实用技巧。

本文目录一览:

Python:尝试使用请求发布表单(python 发请求)

Python:尝试使用请求发布表单(python 发请求)

我正在尝试使用Python和请求库登录网站以进行某些抓取,但我正在尝试以下操作(无效):

import requestsheaders = {''User-Agent'': ''Mozilla/5.0''}payload = {''username'':''niceusername'',''password'':''123456''}In [12]: r = requests.post(''https://admin.example.com/login.php'',headers=headers,data=payload)

但是,nada,重定向到登录页面。我需要打开一个会话吗?我执行了错误的POST请求,是否需要加载cookie?还是会话会自动执行?我在这里迷路了,需要一些帮助和解释。

我要登录的网站是php,我是否需要“捕获set-
cookie并设置cookie标头”?如果是这样,我不知道该怎么做。该网页是具有以下内容的表单(如果有帮助):输入:username’‘password’‘id’:’myform’,’action’:“
login.php

一些额外的信息,也许您可​​以在这里看到我所缺少的..

In [13]: r.headersOut[13]: CaseInsensitiveDict({''content-encoding'': ''gzip'', ''transfer-encoding'': ''chunked'', ''set-cookie'': ''PHPSESSID=v233mnt4malhed55lrpc5bp8o1; path=/'',  ''expires'': ''Thu, 19 Nov 1981 08:52:00 GMT'', ''vary'': ''Accept-Encoding'', ''server'': ''nginx'',   ''connection'': ''keep-alive'', ''pragma'': ''no-cache'',    ''cache-control'': ''no-store, no-cache, must-revalidate, post-check=0, pre-check=0'',     ''date'': ''Tue, 24 Dec 2013 10:50:44 GMT'', ''content-type'': ''text/html''})In [14]: r.cookiesOut[14]: <<class ''requests.cookies.RequestsCookieJar''>[Cookie(version=0, name=''PHPSESSID'', value=''v233mnt4malhed55lrpc5bp8o1'', port=None, port_specified=False, domain=''admin.example.com'',  domain_specified=False, domain_initial_dot=False, path=''/'', path_specified=True, secure=False,   expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>

我非常感谢您的帮助,谢谢!

更新,感谢atupal的回答:

    import requestsheaders = {''User-Agent'': ''Mozilla/5.0''}payload = {''username'':''usr'',''pass'':''123''}link    = ''https://admin.example.com/login.php''session = requests.Session()resp    = session.get(link,headers=headers)# did this for first to get the cookies from the page, stored them with next line:cookies = requests.utils.cookiejar_from_dict(requests.utils.dict_from_cookiejar(session.cookies))resp    = session.post(link,headers=headers,data=payload,cookies =cookies)#used firebug to check POST data, password, was actually ''pass'', under ''net'' in param.  #and to move forward from here after is:session.get(link)

答案1

小编典典

您可以使用Session对象

import requestsheaders = {''User-Agent'': ''Mozilla/5.0''}payload = {''username'':''niceusername'',''password'':''123456''}session = requests.Session()session.post(''https://admin.example.com/login.php'',headers=headers,data=payload)# the session instance holds the cookie. So use it to get/post later.# e.g. session.get(''https://example.com/profile'')

python – Pylons:尝试服务时已经使用的地址

python – Pylons:尝试服务时已经使用的地址

我正在运行挂架,我这样做了:
贴片服务器development.ini
它正在运行:5000

但是当我尝试再次运行命令时:
paster serve development.ini

我收到这条消息:
socket.error:[Errno 98]地址已被使用

有任何想法吗?

解决方法:

通常这意味着它仍在运行,但只有在守护进程模式下才会发生.启动后,是否收到命令提示符,或者是否必须使用Ctrl-C停止它?

如果你得到一个命令提示符,它的deamon模式,你必须停止它

paster server development.ini stop

如果您已使用Ctrl-C(当然不是Ctrl-Z)停止它,我不知道.

Python – 尝试使用意外的mimetype解码JSON:

Python – 尝试使用意外的mimetype解码JSON:

我最近从请求交换到aiohttp,因为我无法在asyncio循环中使用它.

交换完美,一切顺利,除了一件事.我的控制台满满的

Attempt to decode JSON with unexpected mimetype:

Attempt to decode JSON with unexpected mimetype: txt/html; charset=utf-8

我的代码也有一个网站列表,它也可以抓取JSON,每个网站都不同,但我的循环基本上是相同的,我在这里简化了它:

PoolName = "http://website.com"
endpoint = "/api/stats"
headers = "headers = {''content-type'': ''text/html''}" #Ive tried "application/json" and no headers
async with aiohttp.get(url=PoolName+endpoint,headers=headers) as hashrate:
                hashrate = await hashrate.json()
endVariable = hashrate[''glrC''][''HASH'']

它工作得很好,连接到站点抓取json并正确设置endVariable.但出于某种原因

Attempt to decode JSON with unexpected mimetype:

每次进入循环时打印.这很烦人,因为它会向控制台打印统计信息,并且每次抓取站点json时它们都会在错误中丢失

有没有办法解决这个错误或隐藏它?

解决方法

aiohttp正在尝试 do the right thing and warn you个不正确的Content-Type,这可能最糟糕地表明你根本没有获得JSON数据,而是一些不相关的东西,例如错误页面的HTML内容.

但是,在实践中,许多服务器配置错误,总是在其JSON响应中发送不正确的MIME类型,而JavaScript库显然并不关心.如果你知道你正在处理这样的服务器,你可以通过自己调用json.loads来使警告静音:

import json
# ...

async with self._session.get(uri,...) as resp:
    data = await resp.read()
hashrate = json.loads(data)

在您尝试时指定Content-Type没有任何区别,因为它只会影响您的请求的Content-Type,而问题在于服务器响应的Content-Type,这是您无法控制的.

Python 连接在第一次尝试使用导入请求时被拒绝

Python 连接在第一次尝试使用导入请求时被拒绝

如何解决Python 连接在第一次尝试使用导入请求时被拒绝?

我在 Debian 10 上尝试了这个简单的代码:

import requests

asn_url = f''https://bgp.he.net/country/BR''
req = requests.get(asn_url)

当我运行时,出现以下错误:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connection.py",line 159,in _new_conn
    (self._dns_host,self.port),self.timeout,**extra_kw)
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py",line 80,in create_connection
    raise err
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py",line 70,in create_connection
    sock.connect(sa)
OSError: [Errno 101] Network is unreachable

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py",line 600,in urlopen
    chunked=chunked)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py",line 343,in _make_request
    self._validate_conn(conn)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py",line 841,in _validate_conn
    conn.connect()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py",line 301,in connect
    conn = self._new_conn()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py",line 168,in _new_conn
    self,"Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f74e5374fd0>: Failed to establish a new connection: [Errno 101] Network is unreachable

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py",line 449,in send
    timeout=timeout
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py",line 638,in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/lib/python3/dist-packages/urllib3/util/retry.py",line 398,in increment
    raise MaxRetryError(_pool,url,error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host=''bgp.he.net'',port=443): Max retries exceeded with url: /country/BR (Caused by NewConnectionError(''<urllib3.connection.VerifiedHTTPSConnection object at 0x7f74e5374fd0>: Failed to establish a new connection: [Errno 101] Network is unreachable''))

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "sock.py",line 14,in <module>
    req = requests.get(asn_url)
  File "/usr/lib/python3/dist-packages/requests/api.py",line 75,in get
    return request(''get'',params=params,**kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py",line 60,in request
    return session.request(method=method,url=url,**kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py",line 533,in request
    resp = self.send(prep,**send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py",line 646,in send
    r = adapter.send(request,**kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py",line 516,in send
    raise ConnectionError(e,request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host=''bgp.he.net'',port=443): Max retries exceeded with url: /country/BR (Caused by NewConnectionError(''<urllib3.connection.VerifiedHTTPSConnection object at 0x7f74e5374fd0>: Failed to establish a new connection: [Errno 101] Network is unreachable''))

我知道它似乎拒绝连接,但为什么呢? 自第一次尝试以来,这是同样的错误

我在 ubuntu 桌面服务器上使用它并且运行良好,但在 Debian 10 上尝试后它不起作用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

python-2.7 – 无法使用python和请求发布到solr服务器

python-2.7 – 无法使用python和请求发布到solr服务器

这是我试图实现的代码: –

import requests
import tornado.ioloop
import tornado.web
import tornado.autoreload
import json

class MainHandler(tornado.web.RequestHandler):
        def get(self):
            payload = [{"id" : "978-0641723445","cat" : ["book","hardcover"],"name" : "The Lightning Thief","author" : "Rick Riordan","series_t" : "Percy Jackson  Olympians","sequence_i" : 1,"genre_s" : "fantasy","inStock" : True,"price" : 12.50,"pages_i" : 384}]
            url = ''http://localhost:8983/solr/update/json''
            headers = {''content-type'' : ''application/json''}
            # files = {''file'': (''books.json'',open(''books.json'',''rb''))}
            timeline = requests.post(url,data = json.dumps(payload),headers = headers)
            self.write(timeline.text)
class QueryHandler(tornado.web.RequestHandler):
        def get(self):
            # timeline = requests.get(''http://localhost:8983/solr/collection1/select?q=a&wt=json&indent=true'')
            payload = {''q'' : ''a'',''wt'' : ''json'',''indent'' : True}
            timeline = requests.get(''http://localhost:8983/solr/collection1/select'',params = payload)
            self.write(timeline.json())
application = tornado.web.Application([
    (r"/",MainHandler),(r"/query",QueryHandler)
])

if __name__ == "__main__":
    application.listen(8888)
    io_loop = tornado.ioloop.IOLoop.instance()
    tornado.autoreload.start(io_loop)
    io_loop.start()

我能够在localhost:8888 / query上查询solr服务器
但是在localhost:8888我试图发布数据,我从solr得到这个回复: –

{
responseHeader: {
status: 0,QTime: 46
}
}

数据未发布到solr服务器.

有什么建议 ??

解决方法

代码在标头中不包含commitWithin信息.参数以毫秒为单位.只有在提交后,才能从Solr搜索数据.以下可以作为将数据POST到solr的示例.将JSON标头与commitWithin时间以及数据作为JSON字符串添加到数据参数

requests.post(“http:// localhost:8983 / solr / collection1 / update?wt = json”,headers = {“Content-Type”:“application / json”},data =''{“add”:{“ doc“:{”id“:14,”log_type“:”debug“,”log_text“:”来自Kimy的调试事务“},”boost“:1.0,”overwrite“:true,”commitWithin“:1000} }“)

回应:

{ “responseHeader”:{ “状态”:0 “QTIME”:128}}

关于Python:尝试使用请求发布表单python 发请求的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于python – Pylons:尝试服务时已经使用的地址、Python – 尝试使用意外的mimetype解码JSON:、Python 连接在第一次尝试使用导入请求时被拒绝、python-2.7 – 无法使用python和请求发布到solr服务器等相关内容,可以在本站寻找。

本文标签: