爬虫入门

新浪网易淘宝等IP地区信息查询开放API接口调用方法

通过IP地址获取对应的地区信息通常有两种方法:
1)自己写程序,解析IP对应的地区信息,需要数据库。
2)根据第三方提供的API查询获取地区信息。

第一种方法,参见文本《通过纯真IP数据库获取IP地址对应的地区信息》,这种方法的维护代价较高,而且对自己的服务器有一定的压力。

随着技术的开放,第三方已经逐渐免费开放相应的API。经过测试,目前网易和新浪提供的较为稳定易用。

import requests
r=requests.post(url='http://ip.taobao.com/service/getIpInfo.php?ip=111.174.77.14')
print(r.json())
m=r.json()
country=m['data']['country']      #爬取的r.json是嵌套列表
region=m['data']['region']
city=m['data']['city']
print(country+region+city)

 运行结果

  Python具有简单、易学、免费、开源、可移植、可扩展、可嵌入、面向对象等优点,而且从事Python开发,所从事的工作机会和工作岗位及工作内容可选择的余地很多,未来发展空间也很大。∴今天我们来爬取一下51job上python工程师的薪资和工作地点

import requests
from bs4 import BeautifulSoup

import io
import sys
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')

r=requests.get("https://search.51job.com/list/000000,000000,0000,00,9,99,Python%25E5%25BC%2580%25E5%258F%2591%25E5%25B7%25A5%25E7%25A8%258B%25E5%25B8%2588,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=")
print(r.encoding)
r.encoding='gbk'
result=r.text
soup=BeautifulSoup(result,'html5lib')

li=soup.find_all('span',attrs={'class':'t4'})
for l in li:
    print(l.text)
li=soup.find_all('span',attrs={'class':'t3'})
for l in li:
    print(l.text)
原文地址:https://www.cnblogs.com/wt714/p/11822747.html