How to get multiple ip's from a domain loop [duplicate] - python

This question already has answers here:
How can I find all matches to a regular expression in Python?
(1 answer)
Writing a list to a file with Python, with newlines
(26 answers)
Closed 4 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
As you can see below there are multiple strings in an array that is going to go through a loop.
from urllib.request import urlopen
import re
This is the website strings that I want to test out to get the IP from.
exist = ["https://www.youtube.com/", "https://www.twitch.tv/", 'https://twitter.com/']
for c in exist:
try:
def getIP():
d = str(urlopen(c).read())
return r.compile(r'Address: (\d+\.\d+\.\d+\.\d+)').search(d).group(1)
print(getIP())
except AttributeError:
print('error')
But after trying to run the loop it only says Certificate verified failed. is there a reason on why it outputs like that?
Thank you!

Related

Extract email from string variable using find() [duplicate]

This question already has answers here:
Extract email sub-strings from large document
(14 answers)
Closed 3 years ago.
Do you guys know how I'll be able to extract an email from a string using find()
info = "message email#gmail.com"
I want to be able to get the entire "email#gmail.com" and output only that to the screen.
You can do this by using regex:
import re
emails_list = re.findall('\S+#\S+', info)

regex getting url link [duplicate]

This question already has answers here:
Why can't Python parse this JSON data? [closed]
(3 answers)
Closed 5 years ago.
I am trying to extract the link I am getting from a curl command. Curl command throws back of type string.
{"success":true,"key":"Syv77d","link":"https://file.io/Syv77d","expiry":"14 days"}
In my below code this gets https://file.io/Syv77d","expiry":"14 days"}
link = re.search('https://.*$',fileIO)
What I wanted was just https://file.io/Syv77d
The link would vary so i would need the url without the double-qoutes. I think I am missing something in my regex.
Convert the string object to a JSON object.
Ex:
import json
jData = json.loads('{"success":true,"key":"Syv77d","link":"https://file.io/Syv77d","expiry":"14 days"}')
jData["link"]

Python, can someone explain why the return statement only prints the first object? [duplicate]

This question already has answers here:
What does return mean in Python? [closed]
(2 answers)
Why is "None" printed after my function's output?
(7 answers)
Python: Why "return" won´t print out all list elements in a simple for loop and "print" will do it?
(4 answers)
Closed 5 years ago.
I have been practising python and have a small question. I am working with DNA sequences and so I simply wanted to make a small function that just returned the record.ids.
from Bio import AlignIO
my_alignment = Align.IO.read("multipleseqfile.fa","fasta")
def get_id_names(alignment):
for record in alignment:
return record.id
print get_id_names(my_alignment)
I had done a for loop before that prints the names nicely but I wanted to improve my script and make these exercises into functions. However, when I use this function, it only returns the first record id (and there is a list of 30-40). I switched the return record.id to print record.id, and it does print all the names but then I get a None at the end of the output. Not sure what is going on here?

Parsing a URL using regular expression [duplicate]

This question already has answers here:
What's the u prefix in a Python string?
(5 answers)
Closed 6 years ago.
I am trying to parse the 'Meghan' part from the line:
link = http://python-data.dr-chuck.net/known_by_Meghan.html
...with the following regex:
print re.findall('by_(\S+).html$',link)
I am getting the output:
[u'Meghan']
Why I am getting the 'u'?
It means unicode. Depending on what you'll do with it, you can ignore it for the most part, of you can convert it to ascii by doing .encode('ascii')

Python Request in unicode [duplicate]

This question already has answers here:
urllib.quote() throws KeyError
(3 answers)
Closed 7 years ago.
I am trying to make a program that requests to steam to get a the cheapest price for an item. For this I will be using StatTrak™ P250 | Supernova (Factory New) as an example.
The problem is that when requesting, you will make a url:
http://www.steamcommunity.com/market/priceoverview/?country=SG&currency=13&appid=730&market_hash_name=StatTrak™%20P250%20%7C%20Supernova%20%28Factory%20New%29
Afterwards, (I am using the requests module) I do this:
url = "http://www.steamcommunity.com/market/priceoverview/?country=SG&currency=13&appid=730&market_hash_name=StatTrak™%20P250%20%7C%20Supernova%20%28Factory%20New%29"
requests.get(url)
However, the server will return an error.
I can't seem to find solutions to replace ™. I have tried %2122. In python I tried using u'\u084a' but that didn't work too. The problem is that python sends literally \u084a in the request. Is there any way to solve this?
Just use URL encoding. You can't use unicode in urls.
>>> import urllib
>>> f = {'market_hash_name': 'StatTrak™'}
>>> urllib.urlencode(f)
'market_hash_name=StatTrak%E2%84%A2'
Also possible
>>> urllib.quote_plus('StatTrak™')

Categories