我对python非常陌生,我正在尝试写一些东西,从Google的“我感到幸运”按钮中得到第一个结果。我有一个100个项目的清单,我需要它来获得urls。我现在拥有的是:
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时都会得到错误,而不是硬编码的字符串。
发布于 2015-01-26 01:00:40
具体来说,我猜你得到的是:
requests.exceptions.InvalidSchema: No connection adapters were found for 'r'http://www.google.com/search?q="foo"&btnI''(除了其他代替foo的内容:-)
问题很明显是导致r',它确实使字符串成为无效的模式(尾随的'也没有帮助)。
因此,可以尝试这样的方法:
temp1 = 'http://www.google.com/search?q="'
temp2 = '"&btnI'事情应该变得更好..。具体来说,当我这样做时(仍然使用'foo'而不是真正的temp3),我就会得到
http://en.wikipedia.org/wiki/Foobar这似乎对“foo”的顶级搜索结果有意义!-)
https://stackoverflow.com/questions/28143395
复制相似问题