Python爬虫
要引入的第三方库
import urllib.request
import urllib.parse
import string
1.1.先找url
what = input("请输入你想要搜索的东西:")
search = "http://www.baidu.com/s?wd="
1.2.因为python解释器没办法解析中文,所以要将url转码
url = urllib.parse.quote(search + what,safe=string.printable)
2. 向该网站发送请求获取数据
response = urllib.request.urlopen(url)
3. 解析得到的数据
data = response.read().decode("utf-8")
4. 将解析得到的数据保存到本地
with open("what.html","w",encoding="utf-8") as f:
f.write(data)