from urllib.request import urlopen def request_sohu(address): with urlopen(address) as resp: html = resp.read() print(html.decode('utf-8')) if __name__ == "__main__": request_sohu("https://www.sohu.com")可以用内置的urllib.request方法,也可以用第三方包requests
import urllib.request url="http://www.sohu.com/" headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} req = urllib.request.Request(url=url,headers=headers) my_data = urllib.request.urlopen(req) with open('sohu.html', 'wb') as f: read_data = my_data.read() f.write(read_data)