requests.get返回<response>

时间:2016-08-15 15:54:27

标签: python parsing url request python-requests

我遇到了requests.get()的问题。 如果我将URL手动粘贴到代码中,如下所示:

r = requests.get('https://berlin.kauperts.de/Strassen/Igelsteig-12557-Berlin.html')
print(r)

我得到:<Response [200]>。哪个工作得很好。 相反,如果我尝试从文件列表中请求请求,如:

indir = '/home/d/Desktop/civiv_hacking/Streetnames/dump/berlin.kauperts.de/Strassen'
for root, dirs, filenames in os.walk(indir):
  for z in filenames:
     x = urlparse.urljoin('https://berlin.kauperts.de/Strassen/', z+'/'),
     [t[0] for t in x],
     print(t),
     for r in requests.get(t):              
        print(r),

我得到:https://berlin.kauperts.de/Strassen/Igelsteig-12557-Berlin/ <!DOCTYPE html> <html lang="de" xml:lang="de" xmlns="http://www.w3.org/1999/xhtml"> 如何使request.get()返回<Response [200]>而不仅仅是doctype信息?

1 个答案:

答案 0 :(得分:1)

如果你想查看Response 字符串表示,你不应该迭代它,只需打印:

url = urlparse.urljoin('https://berlin.kauperts.de/Strassen/', z)
response = requests.get(url)
print(response)  # would print "<Response [200]>"