如何解决无法连接“ str”和“ file”对象错误?

时间:2019-09-21 23:50:28

标签: python python-2.7

当我给它一个文本文件中的子域列表时,我想制作一个python脚本来自动拍摄子域的屏幕截图。

首先,我学习了python基础知识,然后开始搜索如何执行此操作 当我来到这段代码时:

import requests

BASE = 'https://render-tron.appspot.com/screenshot/'
url = 'https://www.google.com'
path = 'target.jpg'
response = requests.get(BASE + url, stream=True)
# save file, see https://stackoverflow.com/a/13137873/7665691
if response.status_code == 200:
    with open(path, 'wb') as file:
        for chunk in response:
            file.write(chunk)

但是,正如我之前说的,我想给它一个子域列表并逐个检查它, 所以我将此代码编辑为:

import requests

BASE = 'https://render-tron.appspot.com/screenshot/'
url = open('s.txt','r')
path = 'target.jpg'
response = requests.get(BASE + url, stream=True)
# save file, see https://stackoverflow.com/a/13137873/7665691
if response.status_code == 200:
    with open(path, 'wb') as file:
        for chunk in response:
            file.write(chunk)

但是当我运行它时,会给我这个错误:

 Traceback (most recent call last):
  File "ping.py", line 7, in <module>
    response = requests.get(BASE + url, stream=True)
TypeError: cannot concatenate 'str' and 'file' objects

这是我运行的代码:

import requests

BASE = 'https://render-tron.appspot.com/screenshot/'
url = open('s.txt','r')
path = 'target.jpg'
response = requests.get(BASE + url, stream=True)
# save file, see https://stackoverflow.com/a/13137873/7665691
if response.status_code == 200:
    with open(path, 'wb') as file:
        for chunk in response:
            file.write(chunk)

2 个答案:

答案 0 :(得分:0)

如错误所示,尝试将字符串和文件对象组合在一起是没有任何意义的。

您是要访问文件的内容,而不是文件对象本身吗?

如果是这样,请使用:

url = open('s.txt','r').read().strip()

答案 1 :(得分:0)

import requests

BASE = 'https://render-tron.appspot.com/screenshot/'
url = 'https://www.google.com'
path = 'target.jpg'
response = requests.get(BASE + url, stream=True)
# save file, see https://stackoverflow.com/a/13137873/7665691
if response.status_code == 200:
    with open(path, 'wb') as file:
        for chunk in response:
            file.write(chunk)

上面的代码用于仅从URL获取照片并将其保存target.jpg。但据我了解,您正在尝试做的是

您将拥有类似的东西

url1
url2
..

在文件中,而您想要获得

https://render-tron.appspot.com/screenshot/url1
https://render-tron.appspot.com/screenshot/url2
...

并将其保存在您的计算机中。

执行此操作的方法是使用以下代码。

import requests
import os

BASE = 'https://render-tron.appspot.com/screenshot/'
# open the file that you have all the url's in read mode
f = open("urls.txt",'r')
# read all the url and put them in an array. 
urls = f.readlines()

# maintain a count for naming screenshot. Because if we save all images in in target.jpg
# we will end up getting only one image at after the code is executed. Because same file 
# will be overridden over and over again. Instead we plan to save them in 
# screenshot_1.jpg, screenshot_2.jpg etc. 
count = 0
for url in urls:
    response = requests.get(BASE + url, stream=True)
    if response.status_code == 200:
        # make the path for saving the image and store it it target_path
        target_path = os.path.join(os.getcwd(), 'screenshot_%d.jpg' % (count))
        with open(target_path, 'wb') as file:
            for chunk in response:
                file.write(chunk)
        # increase the count of files that has been saved
        count += 1

此代码的预期行为是, 如果您有一个名为urls.txt且带有网址的文件

url1
url2

运行此代码后,您将获得两个名为screenshot_1.jpgscreenshot_2.jpg的屏幕截图,分别为url1url2