我正在尝试获取一些维基百科页面的重定向,但是从请求中得到的内容.history对我来说有点奇怪。
如果我做:
>>> request = requests.get("https://en.wikipedia.org/wiki/barcelona", allow_redirects=True)
>>> request.url
u'https://en.wikipedia.org/wiki/Barcelona'
>>> request.history
[<Response [301]>]
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,重定向是正确的,并且我得到的 URL 与.history从浏览器访问页面时得到的 URL 相同。
但如果我这样做:
>>> request = requests.get("https://en.wikipedia.org/wiki/Yardymli_Rayon", allow_redirects=True)
>>> request.url
u'https://en.wikipedia.org/wiki/Yardymli_Rayon'
>>> request.history
[]
Run Code Online (Sandbox Code Playgroud)
它.history是空的,但在浏览器中我看到 URL 实际上已更改为: https: //en.wikipedia.org/wiki/Yardymli_District
有人知道如何解决吗?
我有一个 python 脚本,用于检查流行的比特币挖矿网站上的再投资设置,我想使用 Pyinstaller 创建一个 Windows .exe 文件进行分发。但是,在我传递电子邮件和密码的有效负载部分中,用户将无法对其进行编辑。因此,我需要能够生成 .exe 和配置文件,用户可以在其中编辑其电子邮件和密码信息。
我正在使用请求(会话),示例有效负载如下所示:
payload = {
'action': 'dashboard.php',
'email': 'user@email.com',
'password': 'abc123-pass'
}
Run Code Online (Sandbox Code Playgroud)
我需要将它们更改为变量,并在执行 exe 时从配置文件中读取它们。
当我在Windows8.1上的Python3.4中运行以下代码时:
r = requests.get(url)
Run Code Online (Sandbox Code Playgroud)
除其他外,它还返回:
File "C:\Python34\lib\site-packages\requests\sessions.py", line 644, in get_adapter
raise InvalidSchema("No connection adapters were found for '%s'" % url) requests.exceptions.InvalidSchema: No connection adapters were found for '"http://example.com"'
Run Code Online (Sandbox Code Playgroud)
这仅仅是我的代码、防火墙或其他问题的问题吗?我该如何修复它?
我正在尝试使用网络挂钩将图像发布到 Slack 频道。这个基本设置允许我将文本发布到频道,但我无法发布图像。这是我的代码:
def posting():
import requests
import json
url = 'https://webhook'
image = {'media': open('trial.jpg', 'rb')}
r = requests.post(url, files=image)
r.json
Run Code Online (Sandbox Code Playgroud)
当我发布文本时,频道中会出现一个网络钩子机器人并将其发布。我需要进一步的身份验证才能发布吗?或者是 Slack 拥有自己的上传 API 并希望我完成该操作?或者机器人无权发布图像的东西?
我在这里查看了其他一些问题,但它们似乎没有使用网络挂钩或机器人,所以我不确定我的问题是否涉及这些问题。
我正在requestsPython中使用包来调用一些API。我位于公司防火墙后面,因此我使用代理来访问 API 中的数据。但现在我还需要使用 OAuth2。Requests本身不支持 OAuth2,我们必须使用requests-oauthlib它。但requests-oauthlib没有使用代理的选项(请参阅此处的文档)。
那么,如何同时使用 OAuth2 和代理呢?尽管我更喜欢使用requests包,因为它简单,但任何方法都是受欢迎的。
我在脚本中使用此函数来请求网页的 BeautifoulSoup 对象:
def getSoup(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
}
i = 0
while i == 0:
print '(%s) (INFO) Connecting to: %s ...' % (getTime(), url)
data = requests.get(url, headers=headers).text
soup = BeautifulSoup(data, 'lxml')
if soup == None:
print '(%s) (WARN) Received \'None\' BeautifulSoup object, retrying in 5 seconds ...' % getTime()
time.sleep(5)
else:
i = 1
return soup
Run Code Online (Sandbox Code Playgroud)
这个循环直到我收到一个有效的 BeautifulSoup 对象,但我想我也可以收到一个不完整的网页,但仍然有一个有效的 BeautifulSoup 对象。我想使用类似的东西:
if '</hml>' …Run Code Online (Sandbox Code Playgroud) 我在 php 上使用 MongoDB。当我进行查找时,会返回 MongoDB\Driver\Cursor。
迭代检索到的数据的更好方法是什么?
foreach($cursor as $entry){}
Run Code Online (Sandbox Code Playgroud)
或者
foreach($cursor->toArray() as $entry){}
Run Code Online (Sandbox Code Playgroud)
每种方法向服务器发送了多少个请求?如果不止一个,是否有一种方法可以通过一个请求检索所有数据?
提前致谢。
我不确定使用什么代码来单击“显示更多”按钮。我想获得正在研究某个主题的大学名单。以下是其中一个网站
http://www.sciencedirect.com/science/article/
我们将非常感谢您的帮助
谢谢
我很新pyhton。我只是尝试一种简单的方法来HTTP使用 python 来从声纳 Web API 中获取简单的响应
我使用请求库并尝试简单的使用:
project = requests.get(url=Sonar_Api_Projects_Search, params=param_Projects, verify=False, headers={'Authorization': 'token {}'.format(token)})
Run Code Online (Sandbox Code Playgroud)
该请求格式良好,当我在网络浏览器中使用它时工作正常。
但作为回应,我得到了这个奇怪的输出:
{“err_code”:500,“err_msg”:“未定义方法
empty?' for nil:NilClass\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/lib/authenticated_system.rb:132:inlogin_from_basic_auth'\n\torg/jruby/RubyProc.java:290:调用中call'\n\torg/jruby/RubyProc.java:224:in'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/ gems/gems/actionpack-2.3.15/lib/action_controller/http_authentication.rb:126:inauthenticate_with_http_basicauthenticate'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/http_authentication.rb:116:in'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/lib/authenticated_system.rb:129:inlogin_from_basic_auth'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/lib/authenticated_system.rb:11:incurrent_user'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/app/controllers/application_controller.rb:102:inset_user_session'\n\torg/jruby/RubyKernel.java:2223:in发送'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF /gems/gems/activesupport-2.3.15/lib/active_support/callbacks.rb:178evaluate_method'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activesupport-2.3.15/lib/active_support/callbacks.rb:166:in:在通话中'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3。 15/lib/action_controller/filters.rb:225:在call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/filters.rb:629:inrun_before_filters'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/filters.rb :615:在call_filters'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/filters.rb:610:inperform_action_with_filters'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/benchmarking.rb:68:以perform_action_with_benchmark'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activesupport-2.3.15/lib/active_support/core_ext/benchmark.rb:17:in毫秒'\n\ tjar:文件:/D:/sonarqube-5.6.6_20170214/lib/server/jruby-complete-1.7.9.jar!/META-INF/jruby.home/lib/ruby/1.8/benchmark.rb:308:inrealtime'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activesupport-2.3.15/lib/active_support/core_ext/benchmark.rb:17:inms'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/benchmarking.rb:68:inperform_action_with_benchmark'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/rescue.rb:160:inPerform_action_with_rescue'\n\tD:/sonarqube -5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/flash.rb:151:发送perform_action_with_flash'\n\torg/jruby/RubyKernel.java:2223:in'\n\tD:/sonarqube-5.6.6_20170214/web/WEB- INF/gems/gems/actionpack-2.3.15/lib/action_controller/base.rb:532:inprocess'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/filters.rb:606:inprocess_with_filters'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3 .15/lib/action_controller/base.rb:391:调用中process'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/base.rb:386:in'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/routing/ route_set.rb:450:在call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/dispatcher.rb:87:in调度'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/dispatcher.rb:85:在dispatch'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/dispatcher.rb:121:in_call' \n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/dispatcher.rb:130:调用build_middleware_stack'\n\torg/jruby/RubyProc.java:290:in'\n\torg/jruby/RubyProc。 java:224:call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/query_cache.rb:29:in调用'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/query_cache.rb:34:incache'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/query_cache.rb:9:in缓存'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/query_cache.rb:28:call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/activerecord-2.3.15/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in调用中'\n\tD:/sonarqube -5.6.6_20170214/web/WEB-INF/config/environment.rb:67:调用中call'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/actionpack-2.3.15/lib/action_controller/string_coercion.rb:25:in'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/rack-1.1.6/ lib/rack/head.rb:9:在上下文中'\n\tD:/sonarqube-5.6.6_20170214/web/WEB-INF/gems/gems/rack-1.1.6/lib/rack/session/abstract/id .rb:58:in …
我有一个包含几千个 URL 的列表,并注意到其中一个在传递SSLError到requests.get(). 下面是我尝试使用此类似问题中建议的解决方案以及使用“try & except”块捕获错误的失败尝试来解决此问题ssl.SSLError:
url = 'https://archyworldys.com/lidl-recalls-puff-pastry/'
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
try:
response = session.get(url,allow_redirects=False,verify=True)
except ssl.SSLError:
pass
Run Code Online (Sandbox Code Playgroud)
最后返回的错误是:
SSLError: HTTPSConnectionPool(host='archyworldys.com', port=443): Max retries exceeded with url: /lidl-recalls-puff-pastry/ (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],)",),))
Run Code Online (Sandbox Code Playgroud)
当我在 Chrome 中打开该 URL 时,收到阻止该网页的“不安全”/“隐私错误”。但是,如果我尝试使用 HTTP 而不是 HTTPS 的 URL(例如“ http://archyworldys.com/lidl-recalls-puff-pastry/ ”),它在我的浏览器中工作得很好。根据这个问题,设置verify可以False解决问题,但我更喜欢找到更安全的解决方法。
虽然我知道一个简单的解决方案是从我的数据中删除该 URL,但我正在尝试找到一种解决方案,让我可以for loop …
python-requests ×10
python ×9
api ×1
bots ×1
exe ×1
mongodb ×1
oauth-2.0 ×1
php ×1
post ×1
proxy ×1
pyinstaller ×1
python-3.4 ×1
python-3.x ×1
redirect ×1
slack-api ×1
sonarqube ×1
ssl ×1
web-scraping ×1
webhooks ×1
wikipedia ×1