1.图片爬取
网络图片的格式:
http://www.example.com/picture.jpg
代码示例:
```python
import requests
import os
url = "http://****"
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("爬取失败")
2.搜索引擎的爬取
baidu关键词接口:
http://www.baidu.com/s?wd=keyword
360 关键词接口:
http://www.so.com/s?q=keyword
code:
import requests
try:
kv = {'wd':'python'}
r = requests.get("http://www.baidu.com/s",params = kv)
print(r.request.url)
r.raise_for_status()
print(len(r.text))
except:
print("爬取失败")