【问题标题】:python requests.get() InvalidSchema errorpython requests.get() InvalidSchema 错误
【发布时间】:2015-03-24 11:15:22
【问题描述】:

我对 python 非常陌生,我正在尝试编写一些东西来获得从 Google 的“我感觉很幸运”按钮返回的第一个结果。我有一个包含 100 个项目的列表,我需要它来获取 url。这是我所拥有的:

import requests

with open('2012.txt') as f:
    lines = f.readlines()

for i in range(0, 100):
    temp1 = "r'http://www.google.com/search?q=\""
    temp2 = "\"&btnI'"
    temp3 = lines[i]
    temp3 = temp3[:-1]
    temp4 = temp1+temp3+temp2
    print temp4
    var = requests.get(temp4)
    print var.url

现在,如果我打印 temp4 中的值并将其粘贴到 requests.get() 中,它会按我的意愿工作。但是,每次尝试传递 temp4 而不是硬编码的字符串时,我都会收到错误。

【问题讨论】:

  • 刚刚编辑了 Q 以使代码可读 - 突出显示它并单击看起来有点像 {} 的图标(“代码块”我想如果你悬停它会说)。

标签: python python-2.7 python-requests


【解决方案1】:

具体来说,我猜你会得到:

requests.exceptions.InvalidSchema: No connection adapters were found for 'r'http://www.google.com/search?q="foo"&btnI''

(除了用其他东西代替 foo:-) -- 请在 Q 中发布例外情况,为什么让我们猜测或需要重现?!

问题显然是前导 r' 确实使字符串变为无效架构(尾随 ' 也无济于事)。

所以,试试类似的东西:

temp1 = 'http://www.google.com/search?q="'
temp2 = '"&btnI'

事情应该会变得更好......具体来说,当我这样做时(仍然用'foo'代替真正的temp3),我明白了

http://en.wikipedia.org/wiki/Foobar

这似乎是“foo”的热门搜索结果!-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多