Python爬虫入门之get网页信息并作为文本输出
import requests
import os
kv = {
"User-agent":"Mozilla/5.0"#模拟浏览器
}
def getHtmlText(url):
try:
r = requests.get(url,timeout = 30,headers = kv)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return "ERROR"
url = input("Please input the URL!\n")
print(getHtmlText(url))
os.system("pause")