import requests url=“https://www.sogou.com/web?query=周杰伦” headers={ “User-Agent”:“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55” } #以字典的形式设置请求头,处理反爬 resp=requests.get(url,headers=headers) print(resp) #结果:<Response [200]> print(resp.text) #拿到页面源代码 resp.close() #关掉resp
import requests query=input(“请输入一个你喜欢的明星:”) url=f"https://www.sogou.com/web?query={query}" headers={ “User-Agent”:“Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55” } resp=requests.get(url,headers=headers) print(resp) print(resp.text) #拿到页面源代码 resp.close() #关闭resp
|