简体   繁体   中英

Python HTML source code

I would like to write a script that picks a special point from the source code and returns it. (print it)

import urllib.request                           

Webseite = "http://myip.is/"                    
html_code = urllib.request.urlopen(Webseite)

print(html_code.read().decode('ISO-8859-1'))

This is my current code. I would like to print only the IP address that the website gives. The input of this I will print in python (title="copy ip address").

You could use jsonip which returns a JSON object that you can easily parse using standard Python library

import json
from urllib2 import urlopen

my_ip = json.load(urlopen('http://jsonip.com'))['ip']
import requests
from bs4 import BeautifulSoup

s = requests.Session()
r = s.get('http://myip.is/')

soup = BeautifulSoup(r.text, "html5lib")
myIP = mySoup.find('a', {'title': 'copy ip address'}).text
print(myIP)

This uses the requests library (which you should always use for HTTP requests) to pull the page, feeds the content to BeautifulSoup, a very nice HTML parser, and asks BeautifulSoup to find a single <a> tag, with the atrtibuet title set to 'copy ip address', and then save the text component of that tag as myIP .

You can use a regular expression to find the IP addresses:

import urllib.request
import re

Webseite = "http://myip.is/"
html_code = urllib.request.urlopen(Webseite)

content = html_code.read().decode('ISO-8859-1')
ip_regex = r'(?:[0-9]{1,3}\.){3}[0-9]{1,3}'

ips_found = re.findall(ip_regex, content)
print(ips_found[0])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM