Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Web Scraping with Python/Chapter4/get_wiki_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
import random
import re
import json
import ssl


context = ssl._create_unvarified_context()
random.seed(datetime.datetime.now())

class IPv4_Address_Info:
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)

return responseJson.data.country


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"})
Expand All @@ -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",
Expand All @@ -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")

Expand Down