Python 普通爬虫2


import urllib.request
import ssl
import json

def ajaxCrawler(url):
headers = {
"User-Agnet": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)" \
" Chrome/56.0.2924.90 Safari/537.36 2345Explorer/9.6.0.18627"}
req=urllib.request.Request(url,headers=headers)
#使用ssl创建未认证的上下文
context=ssl._create_unverified_context()#爬https网页用

response=urllib.request.urlopen(req,context=context)

jsonStr=response.read().decode("utf-8")
jsonData=json.loads(jsonStr)#返回字典类型
return jsonData

url="https://www.qiushibaike.com/text/page/1/"
info=ajaxCrawler(url)
print(info)
posted @ 2019-03-13 19:30  飞飞阿  阅读(172)  评论(0编辑  收藏  举报