第1个爬虫程序: main.py
from urllib.request import urlopen
url = "https://www.hao123.com/"
if __name__ == '__main__':
resp = urlopen(url)
with open("myGetFile.html", mode="w", encoding='utf-8') as f:
f.write(resp.read().decode('utf-8'))
f.close()
resp.close()
print("结束")
第2个爬虫程序: main2.py
import requests
if __name__ == '__main__':
query = input("请输入一个你喜欢的明星:")
url = f"https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&srcqid=5655130659909863611&tn=50000021_hao_pg&wd={query}"
dic = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.47"}
resp = requests.get(url, headers=dic)
with open("myGetFile.html", mode="w", encoding='utf-8') as f:
f.write(resp.text)
f.close()
resp.close()
print("结束")
关注公众号,获取更多资料
|