From 05be6a37b636bee92a4c2b9d8a1f68bf21f44cec Mon Sep 17 00:00:00 2001 From: OswaldHe Date: Thu, 13 Jul 2017 17:28:00 +0800 Subject: [PATCH] Update get_wiki_ip.py Avoid Certificate Error for Python 3 --- Web Scraping with Python/Chapter4/get_wiki_ip.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Web Scraping with Python/Chapter4/get_wiki_ip.py b/Web Scraping with Python/Chapter4/get_wiki_ip.py index 4472917..a77b79c 100644 --- a/Web Scraping with Python/Chapter4/get_wiki_ip.py +++ b/Web Scraping with Python/Chapter4/get_wiki_ip.py @@ -4,8 +4,9 @@ import random import re import json +import ssl - +context = ssl._create_unvarified_context() random.seed(datetime.datetime.now()) class IPv4_Address_Info: @@ -13,7 +14,7 @@ def __init__(self, d): self.__dict__ = d def getIPv4Info(ipv4Address): - response = urlopen("http://ip.taobao.com/service/getIpInfo.php?ip="+ipv4Address).read().decode('utf-8') + response = urlopen("http://ip.taobao.com/service/getIpInfo.php?ip="+ipv4Address, context = context).read().decode('utf-8') responseJson = json.loads(response, object_hook=IPv4_Address_Info) @@ -21,7 +22,7 @@ def getIPv4Info(ipv4Address): def getIPv6Info(ipv6Address): - html = urlopen("http://geoip.neu.edu.cn/?ip=" + ipv6Address) + html = urlopen("http://geoip.neu.edu.cn/?ip=" + ipv6Address, context = context) data = BeautifulSoup(html, "html.parser") city = data.findAll("p", {"class": "text-info lead"}) @@ -30,7 +31,7 @@ def getIPv6Info(ipv6Address): def getLinks(articleUrl): - html = urlopen("http://en.wikipedia.org" + articleUrl) + html = urlopen("http://en.wikipedia.org" + articleUrl, context = context) data = BeautifulSoup(html, "html.parser") return data.find("div", {"id":"bodyContent"}).findAll("a", @@ -43,7 +44,7 @@ def getHistoryIPs(pageUrl): print("history url is:" + historyUrl) - html = urlopen(historyUrl) + html = urlopen(historyUrl, context = context) data = BeautifulSoup(html, "html.parser")