1. 笔记视频来源
- https://www.bilibili.com/video/BV1pt41137qK?p=16
2. 搜索引擎关键字提交接口
- 百度的关键词接口
http://www.baidu.com/s?wd=关键字 - 360的关键词接口
http://www.so.com/s?q=关键字
3. 实例代码
原理就是使用 requests.get函数,将url进行网址和搜索关键字的拼接。
其中python的字典可以理解为java中的键值对。学了几节课程,高频听到字典这个词。实际代码也经常出现。例如params = {‘params’: baidu}。 requests.get中的可选参数也是要求传入字典类型数据
import requests
def searchBaidu():
try:
# 百度搜索关键字的键值对
baidu = {'wd': 'Python'}
params = {'params': baidu}
# 拼接字符串:http://www.baidu.com/s?wd=关键字
r = requests.get("http://www.baidu.com/s", **params)
r.raise_for_status()
r.encoding = r.apparent_encoding
print('searchBaidu')
print(r.status_code)
print(r.url)
print(len(r.text))
except:
print("爬取失败:status_code", r.status_code, r.request.headers)
def search360():
try:
# 360搜索关键字的键值对
s360 = {'q': 'Python'}
params = {'params': s360}
# 拼接字符串:http://www.so.com/s?q=关键字http://www.so.com/s?q=关键字
r = requests.get("http://www.so.com/s", **params)
r.raise_for_status()
r.encoding = r.apparent_encoding
print('search360')
print(r.status_code)
print(r.url)
print(len(r.text))
except:
print("爬取失败:status_code", r.status_code, r.request.headers)
if __name__ == '__main__':
searchBaidu()
search360()
4. 运行结果
C:\Users\珞落\AppData\Local\Programs\Python\Python39\python.exe D:/PythonProject/main.py
searchBaidu
200
http://www.baidu.com/s?wd=Python
812748
search360
200
https://www.so.com/s?q=Python
486002
Process finished with exit code 0
|