[英]Python HTML source code
我想编写一个脚本,从源代码中选择一个特殊点并返回它。 (打印出来)
import urllib.request
Webseite = "http://myip.is/"
html_code = urllib.request.urlopen(Webseite)
print(html_code.read().decode('ISO-8859-1'))
这是我目前的代码。 我只想打印网站提供的 IP 地址。 这个输入我会用python打印出来(title="copy ip address")。
您可以使用jsonip返回一个 JSON 对象,您可以使用标准 Python 库轻松解析该对象
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)
这使用请求库(您应该始终用于 HTTP 请求)来拉页面,将内容提供给 BeautifulSoup,一个非常好的 HTML 解析器,并要求 BeautifulSoup 找到一个<a>
标签,将属性title
设置为'copy ip address',然后将该标签的文本部分保存为myIP
。
您可以使用正则表达式来查找 IP 地址:
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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.