0

I want to make a python script to take a screenshots of subdomains automatically when I give it a list of subdomains in text file.

First I learnt python basics then started searching for how to do this when I came to this code:

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)

But, as I said before, I want to give it a list of subdomains and check it one by one, so I edited this code to be:

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)

But when I ran it it give me this error:

 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

and this is the code I ran:

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)
martineau
  • 119,623
  • 25
  • 170
  • 301
ELMO
  • 581
  • 2
  • 5
  • 7

2 Answers2

0

As the error says, it makes no sense to try to combine a string and a file object.

Did you mean to access the contents of the file, rather than the file object itself?

If so, use this:

url = open('s.txt','r').read().strip()
John Gordon
  • 29,573
  • 7
  • 33
  • 58
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)

The code above is for getting just a photo from a url and saving it target.jpg. But what I understand is, what you are trying to do is,

You will have have something like

url1
url2
..

in a file, and you want to get

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

and save it in your computer.

The way you can do this is using the following code.

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

The expected behavior of this code will be, If you have a file named urls.txt with urls

url1
url2

after running this code, you will have two screenshots named screenshot_1.jpg and screenshot_2.jpg with screenshot of url1 and url2

Nadim Abrar
  • 186
  • 1
  • 7