url = ''
try:r = requests.get(url)r.raise_for_status()r.encoding = r.apparent_encodingprint(r.text[:1000])
except:print('爬取失败')
url = ''
try:kv = {'user-agent':'Mozilla/5.0'}r = requests.get(url, headers=kv) r.raise_for_status()r.encoding = r.apparent_encodingprint(r.text[1000:2000])
except:print('爬取失败')
keyword = 'python'
try:kv = {'wd': keyword}r = requests.get('http://www.baidu.com/s', params=kv)print(r.request.url)r.raise_for_status()print(len(r.text))
except:print('爬取失败')
keyword = 'python'
try:kv = {'q': keyword}r = requests.get('http://www.so.com/s', params=kv)print(r.request.url)r.raise_for_status()print(len(r.text))
except:print('爬取失败')
url = ''
root = 'D://PICS//'
path = root + url.split('/')[-1]
try:if not os.path.exists(root):os.mkdir(root) if not os.path.exists(path):r = requests.get(url)with open(path, 'wb') as f:f.write(r.content)f.close()print('文件保存成功')else:print('文件已存在')
except:print('爬取失败')
url = 'http://m.ip138.com/ip.asp?ip=ipaddress'
try:r = requests.get(url + 'IP')r.raise_for_status()r.encoding = r.apparent_encodingprint(r.text[-500:])
except:print('爬取失败')