import sysimport osimport requestsfrom bs4 import BeautifulSoupimport socketimport reimport pandas as pdimport random import time#匹配ip地址def matchip (ip):url = "/"try:url = url+str(ip)except:print(url)#time.sleep(random.uniform(1,3.5)) #每隔1至3.5秒获取一次,频次太高易被反爬wbdata = requests.get(url).textsoup = BeautifulSoup(wbdata, 'lxml')for tag in soup.find_all('span', class_='Whwtdhalf w50-0'):tag_extractl = tag.get_text()if tag_extractl.find("IP的物理位置"):#过滤'IP的物理位置'字符串l_ip.append(ip)l_location.append(tag_extractl)if __name__ == '__main__':l_ip=list()l_location=list()data=pd.read_csv('in.csv')i=0for ip in list(data.user_ip):i=i+1print('iter',i)matchip(ip)df=pd.DataFrame({'ip':l_ip,'地区':l_location}) df.to_csv('out.csv')
数据格式,如192.168.0.1
输出格式如下,