# -*- coding: utf-8 -*
import requests
import random
UAPOOLS=['Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0',
'Mozilla/5.0 (Windows NT 6.1; rv:49.0) Gecko/20100101 Firefox/49.0',
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0'
] #头部信息列表池
def get_one_page(url):
try:
head = {}
head['User-Agent'] = random.choice(UAPOOLS) # 从序列中随机选择一个元素,写入User Agent信息
response = requests.get(url,headers=head) # 打开网址
if response.status_code == 200: # 判断状态码
return response.text # 状态码成功返回网页源代码
return None # 状态码其他结果返回None
except RequestException:
print('请求ip代理页面出错')
return None
def main():
url = 'http://www.xicidaili.com/nt'
page=get_one_page(url)
print(page)
if __name__ == '__main__':
main()
头部信息列表池
最新推荐文章于 2022-10-12 13:24:46 发布