send post request from .txt file - python
I'm new in Python and looking for some help :)
I created simple script which is checking IP reputation (from lists.txt) in IPVoid:
import requests
import re
URL = "https://www.ipvoid.com/ip-blacklist-check/"
ip = open('lists.txt')
DATA = {"ip":ip}
r = requests.post(url = URL, data = {"ip":ip})
text = r.text
bad_ones= re.findall(r'<i class="fa fa-minus-circle text-danger" aria-hidden="true"></i> (.+?)</td>', text)
print(bad_ones)
The lists.txt contain list of IPs:
8.8.8.8
4.4.4.4
etc..
However, the script tooks only 1 line of the script - i would like to do "bulk" checking.
please advice :)
It is not clear whether or not the ip addresses in the txt file are organized line by line, but I assume that this is the case.
You can do something like the following
import requests
import re
URL = "https://www.ipvoid.com/ip-blacklist-check/"
bad_ones = []
with open('lists.txt') as f:
for ip in f.readlines():
r = requests.post(url = URL, data = {"ip":ip.strip()})
text = r.text
bad_ones.append(re.findall(r'<i class="fa fa-minus-circle text-danger" aria-hidden="true"></i> (.+?)</td>', text))
print(bad_ones)
The with open('lists.txt') as f statement lets you
open the file and name the resulting io object as f, when the end of the 'with' block is reached the file will be closed without explicitly calling f.close().
Now for the batch mode it is a simple loop over each line of the text file, with a simple filter to get rid of the newline character by calling strip() on each ip string (the line of the text file).
I am not even sure if your above program works.. The ip variable in your program is basically a io object.
What you need is a for loop to send request for each and every IP.
You can do bulk checking if the API accepts them
import requests
import re
URL = "https://www.ipvoid.com/ip-blacklist-check/"
ips = open('lists.txt')
for ip in ips.readlines():
DATA = {"ip":ip}
r = requests.post(url = URL, data = {"ip":ip})
text = r.text
'''Your processing goes here..'''
Also explore using with clause for opening and closing files.
Related
Search for a word in webpage and save to TXT in Python
I am trying to: Load links from a .txt file, search for a specific Word, and if the word exists on that webpage, save the link to another .txt file but i am getting error: No scheme supplied. Perhaps you meant http://<_io.TextIOWrapper name='import.txt' mode='r' encoding='cp1250'>? Note: the links has HTTPS:// The code: import requests list_of_pages = open('import.txt', 'r+') save = open('output.txt', 'a+') word = "Word" save.truncate(0) for page_link in list_of_pages: res = requests.get(list_of_pages) if word in res.text: response = requests.request("POST", url) save.write(str(response) + "\n") Can anyone explain why ? thank you in advance !
Try putting http:// behind the links.
When you use res = requests.get(list_of_pages) you're creating HTTP connection to list_of_pages. But requests.get takes URL string as a parameter (e.g. http://localhost:8080/static/image01.jpg), and look what list_of_pages is - it's an already opened file. Not a string. You have to either use requests library, or file IO API, not both. If you have an already opened file, you don't need to create HTTP request at all. You don't need this request.get(). Parse list_of_pages like a normal, local file. Or, if you would like to go the other way, don't open this text file in list_of_arguments, make it a string with URL of that file.
Can write into the file in python and startswith not working
I have a problem. I have a task "download by ID" This is my previous program which download text (PDB file) from urllib.request import urlopen def download(inf): url = xxxxxxxxxxx response = urlopen(xxx) text = response.read().decode('utf-8') return data new_download = download('154) It works perfect, but my function that I must create, don't write to file lines which starts with num from urllib.request import urlopen #moduule for URL processing with open('new_test', 'w') as a: for sent in text: #for every line in sequences file if line.startswith('num'): line1.writeline(sent)
You're not iterating over the lines, you're iterating over the characters. Change for line in data2: to for line in data2.splitlines():
Save streaming audio from URL as MP3, or even just audio file from URL as MP3
I am trying to have my server, in python 3, go grab files from URLs. Specifically, I would like to pass a URL into a function, I would like the function to go grab an audio file(of many varying formats) and save it as an MP3, probably using ffmpeg or ffmpy. If the URL also has a PDF, I would also like to save that, as a PDF. I haven't done much research on the PDF yet, but I have been working on the audio piece and wasn't sure if this was even possible. I have looked at several questions here, but most notably; How do I download a file over HTTP using Python? It's a little old but I tried several methods in there and always get some sort of issue. I have tried using the requests library, urllib, streamripper, and maybe one other. Is there a way to do this and with a recommended library? For example, most of the ones I have tried do save something, like the html page, or an empty file called 'file.mp3' in this case. Streamripper received a try changing user agents error. I am not sure if this is possible, but I am sure there is something I'm not understanding here, could someone point me in the right direction? This isn't necessarily the code I'm trying to use, just an example of something I have used that doesn't work. import requests url = "http://someurl.com/webcast/something" r = requests.get(url) with open('file.mp3', 'wb') as f: f.write(r.content) # Retrieve HTTP meta-data print(r.status_code) print(r.headers['content-type']) print(r.encoding) **Edit import requests import ffmpy import datetime import os ## THIS SCRIPT CAN BE PASSED A URL AND IF THE URL RETURNS ## HTTP HEADER FOR CONTENT TYPE AUDIO/MPEG, THE FILE WILL ## BE SAVED AS THE CURRENT-DATE-AND-TIME.MP3 ## ## THIS SCRIPT CAN BE PASSED A URL AND IF THE URL RETURNS ## HTTP HEADER FOR CONTENT TYPE application/pdf, THE FILE WILL ## BE SAVED AS THE CURRENT-DATE-AND-TIME.PDF ## ## THIS SCRIPT CAN BE PASSED A URL AND IF THE URL RETURNS ## HTTP HEADER FOR CONTENT TYPE other than application/pdf, OR ## audio/mpeg, THE FILE WILL NOT BE SAVED def BordersPythonDownloader(url): print('Beginning file download requests') r = requests.get(url, stream=True) contype = r.headers['content-type'] if contype == "audio/mpeg": print("audio file") filename = '[{}].mp3'.format(str(datetime.datetime.now())) with open('file.mp3', 'wb+') as f: f.write(r.content) ff = ffmpy.FFmpeg( inputs={'file.mp3': None}, outputs={filename: None} ) ff.run() if os.path.exists('file.mp3'): os.remove('file.mp3') elif contype == "application/pdf": print("pdf file") filename = '[{}].pdf'.format(str(datetime.datetime.now())) with open(filename, 'wb+') as f: f.write(r.content) else: print("URL DID NOT RETURN AN AUDIO OR PDF FILE, IT RETURNED {}".format(contype)) # INSERT YOUR URL FOR TESTING # OR CALL THIS SCRIPT FROM ELSEWHERE, PASSING IT THE URL #DEFINE YOUR URL #url = 'http://archive.org/download/testmp3testfile/mpthreetest.mp3' #CALL THE SCRIPT; PASSING IT YOUR URL #x = BordersPythonDownloader(url) #ANOTHER EXAMPLE WITH A PDF #url = 'https://www.cisco.com/c/en/us/td/docs/switches/lan/catalyst6500/ios/12-2SY/configuration/guide/sy_swcg/etherchannel.pdf' #x = BordersPythonDownloader(url) Thanks Richard, this code works and helps me understand this better. Any suggestions for improving the above working example?
Creating a python program that scrapes file from a website
This is what I have so far import urllib Champions=["Aatrox","Ahri","Akali","Alistar","Amumu","Anivia","Annie","Ashe","Azir","Blitzcrank","Brand","Braum","Caitlyn","Cassiopeia","ChoGath","Corki","Darius","Diana","DrMundo","Draven","Elise","Evelynn","Ezreal","Fiddlesticks","Fiora","Fizz","Galio","Gangplank","Garen","Gnar","Gragas","Graves","Hecarim","Heimerdinger","Irelia","Janna","JarvanIV","Jax","Jayce","Jinx","Kalista","Karma","Karthus","Kassadin","Katarina","Kayle","Kennen","KhaZix","KogMaw","LeBlanc","LeeSin","Leona","Lissandra","Lucian","Lulu","Lux","Malphite","Malzahar","Maokai","MasterYi","MissFortune","Mordekaiser","Morgana","Nami","Nasus","Nautilus","Nidalee","Nocturne","Nunu","Olaf","Orianna","Pantheon","Poppy","Quinn","Rammus","RekSai","Renekton","Rengar","Riven","Rumble","Ryze","Sejuani","Shaco","Shen","Shyvana","Singed","Sion","Sivir","Skarner","Sona","Soraka","Swain","Syndra","Talon","Taric","Teemo","Thresh","Tristana","Trundle","Tryndamere","TwistedFate","Twitch","Udyr","Urgot","Varus","Vayne","Veigar","VelKoz","Vi","Viktor","Vladimir","Volibear","Warwick","Wukong","Xerath","XinZhao","Yasuo","Yorick","Zac","Zed","Ziggs","Zilean","Zyra"] currentCount=0 while currentCount < len(Champions): urllib.urlretrieve("http://www.lolflavor.com/champions/"+Champions[currentCount]+ "/Recommended/"+Champions[currentCount]+"_lane_scrape.json","C:\Users\Jay\Desktop\LolFlavor\ " +Champions[currentCount]+ "\ "+Champions[currentCount]+ "_lane_scrape.json") currentCount+=1 What the program is meant to do is to use the list and the currentCount to get the champion, then it goes to the website e.g for "Aatrox" http://www.lolflavor.com/champions/Aatrox/Recommended/Aatrox_lane_scrape.json, then it downloads and stores the file in the folder LolFlavor/Aatrox/Aatrox_lane_scrape.json in this case. The bit which is Aatrox changes depending on the champion. Can anyone help me to get it to work? EDIT: CURRENT CODE WITH VALUE ERROR: import json import os import requests Champions=["Aatrox","Ahri","Akali","Alistar","Amumu","Anivia","Annie","Ashe","Azir","Blitzcrank","Brand","Braum","Caitlyn","Cassiopeia","ChoGath","Corki","Darius","Diana","DrMundo","Draven","Elise","Evelynn","Ezreal","Fiddlesticks","Fiora","Fizz","Galio","Gangplank","Garen","Gnar","Gragas","Graves","Hecarim","Heimerdinger","Irelia","Janna","JarvanIV","Jax","Jayce","Jinx","Kalista","Karma","Karthus","Kassadin","Katarina","Kayle","Kennen","KhaZix","KogMaw","LeBlanc","LeeSin","Leona","Lissandra","Lucian","Lulu","Lux","Malphite","Malzahar","Maokai","MasterYi","MissFortune","Mordekaiser","Morgana","Nami","Nasus","Nautilus","Nidalee","Nocturne","Nunu","Olaf","Orianna","Pantheon","Poppy","Quinn","Rammus","RekSai","Renekton","Rengar","Riven","Rumble","Ryze","Sejuani","Shaco","Shen","Shyvana","Singed","Sion","Sivir","Skarner","Sona","Soraka","Swain","Syndra","Talon","Taric","Teemo","Thresh","Tristana","Trundle","Tryndamere","TwistedFate","Twitch","Udyr","Urgot","Varus","Vayne","Veigar","VelKoz","Vi","Viktor","Vladimir","Volibear","Warwick","Wukong","Xerath","XinZhao","Yasuo","Yorick","Zac","Zed","Ziggs","Zilean","Zyra"] for champ in Champions: os.makedirs("C:\\Users\\Jay\\Desktop\\LolFlavor\\{}\\Recommended".format(champ), exist_ok=True) with open(r"C:\Users\Jay\Desktop\LolFlavor\{}\Recommended\{}_lane_scrape.json".format(champ,champ),"w") as f: r = requests.get("http://www.lolflavor.com/champions/{}/Recommended/{}_lane_scrape.json".format(champ,champ)) json.dump(r.json(),f) with open(r"C:\Users\Jay\Desktop\LolFlavor\{}\Recommended\{}_jungle_scrape.json".format(champ,champ),"w") as f: r = requests.get("http://www.lolflavor.com/champions/{}/Recommended/{}_jungle_scrape.json".format(champ,champ)) json.dump(r.json(),f) with open(r"C:\Users\Jay\Desktop\LolFlavor\{}\Recommended\{}_support_scrape.json".format(champ,champ),"w") as f: r = requests.get("http://www.lolflavor.com/champions/{}/Recommended/{}_support_scrape.json".format(champ,champ)) json.dump(r.json(),f) with open(r"C:\Users\Jay\Desktop\LolFlavor\{}\Recommended\{}_aram_scrape.json".format(champ,champ),"w") as f: r = requests.get("http://www.lolflavor.com/champions/{}/Recommended/{}_aram_scrape.json".format(champ,champ)) json.dump(r.json(),f)
import requests Champions=["Aatrox","Ahri","Akali","Alistar","Amumu","Anivia","Annie","Ashe","Azir","Blitzcrank","Brand","Braum","Caitlyn","Cassiopeia","ChoGath","Corki","Darius","Diana","DrMundo","Draven","Elise","Evelynn","Ezreal","Fiddlesticks","Fiora","Fizz","Galio","Gangplank","Garen","Gnar","Gragas","Graves","Hecarim","Heimerdinger","Irelia","Janna","JarvanIV","Jax","Jayce","Jinx","Kalista","Karma","Karthus","Kassadin","Katarina","Kayle","Kennen","KhaZix","KogMaw","LeBlanc","LeeSin","Leona","Lissandra","Lucian","Lulu","Lux","Malphite","Malzahar","Maokai","MasterYi","MissFortune","Mordekaiser","Morgana","Nami","Nasus","Nautilus","Nidalee","Nocturne","Nunu","Olaf","Orianna","Pantheon","Poppy","Quinn","Rammus","RekSai","Renekton","Rengar","Riven","Rumble","Ryze","Sejuani","Shaco","Shen","Shyvana","Singed","Sion","Sivir","Skarner","Sona","Soraka","Swain","Syndra","Talon","Taric","Teemo","Thresh","Tristana","Trundle","Tryndamere","TwistedFate","Twitch","Udyr","Urgot","Varus","Vayne","Veigar","VelKoz","Vi","Viktor","Vladimir","Volibear","Warwick","Wukong","Xerath","XinZhao","Yasuo","Yorick","Zac","Zed","Ziggs","Zilean","Zyra"] for champ in Champions: r = requests.get("http://www.lolflavor.com/champions/{}/Recommended/{}_lane_scrape.json".format(champ,champ)) print(r.json()) If you want to save each to a file. dump the json. import json import simplejson for champ in Champions: with open(r"C:\Users\Jay\Desktop\LolFlavor\{}_lane_scrape.json".format(champ),"w") as f: try: r = requests.get("http://www.lolflavor.com/champions/{}/Recommended/{}_lane_scrape.json".format(champ, champ)) json.dump(r.json(),f) except simplejson.scanner.JSONDecodeError as e: print(e.r.url) The error is from 404 - File or directory not found as one of you calls fails so there is no valid json to decode. The offending url is: u'http://www.lolflavor.com/champions/Wukong/Recommended/Wukong_lane_scrape.json' which if you try in your browser will also give you a 404 error. That is caused by the fact there is no user Wukong which can be confirmed by opening http://www.lolflavor.com/champions/Wukong/ in your browser There is no need to index the list using a while loop. simply iterate over the list items directly and use str.format to pass the variables into the url. Also make sure you use raw string r for the file path when using \'s as they have a special meaning in python they are using to escape characters so \n or \r etc.. in your paths would cause problems. You can also use / or escape using \\.
Writing data to csv or text file using python
I am trying to write some data to csv file by checking some condition as below I will have a list of urls in a text file as below urls.txt www.example.com/3gusb_form.aspx?cid=mum www.example_second.com/postpaid_mum.aspx?cid=mum www.example_second.com/feedback.aspx?cid=mum Now i will go through each url from the text file and read the content of the url using urllib2 module in python and will search a string in the entire html page. If the required string founds i will write that url in to a csv file. But when i am trying to write data(url) in to csv file,it is saving like each character in to one coloumn as below instead of saving entire url(data) in to one column h t t p s : / / w w w...... Code.py import urllib2 import csv search_string = 'Listen Capcha' html_urls = open('/path/to/input/file/urls.txt','r').readlines() outputcsv = csv.writer(open('output/path' + 'urls_contaning _%s.csv'%search_string, "wb"),delimiter=',', quoting=csv.QUOTE_MINIMAL) outputcsv.writerow(['URL']) for url in html_urls: url = url.replace('\n','').strip() if not len(url) == 0: req = urllib2.Request(url) response = urllib2.urlopen(req) if str(search_string) in response.read(): outputcsv.writerow(url) So whats wrong with the above code, what needs to be done in order to save the entire url(string) in to one column in a csv file ? Also how can we write data to a text file as above ? Edited Also i had a url suppose like http://www.vodafone.in/Pages/tuesdayoffers_che.aspx , this url will be redirected to http://www.vodafone.in/pages/home_che.aspx?cid=che in browser actually, but when i tried through code as below it is same as the above given url import urllib2, httplib httplib.HTTPConnection.debuglevel = 1 request = urllib2.Request("http://www.vodafone.in/Pages/tuesdayoffers_che.aspx") opener = urllib2.build_opener() f = opener.open(request) print f.geturl() Result http://www.vodafone.in/pages/tuesdayoffers_che.aspx?cid=che So finally how to catch the redirected url with urllib2 and fetch the data from it ?
Change the last line to: outputcsv.writerow([url])