知乎爬取

import requests,json
import mysqlhelper

headers = {
‘User-Agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36’
}

url= ‘https://www.zhihu.com/api/v4/members/nai-tang-43-84/answers?include=data%5B%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Cmark_infos%2Ccreated_time%2Cupdated_time%2Creview_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cvoting%2Cis_author%2Cis_thanked%2Cis_nothelp%3Bdata%5B%5D.author.badge%5B%3F(type%3Dbest_answerer)%5D.topics&offset=0&limit=20&sort_by=created’
response = requests.get(url,headers=headers)

text_dict = response.text
json_dict = json.loads(text_dict)

print(dict1)

print(type(json_dict))

myhelper = mysqlhelper.MysqlHelper()
sql = ‘INSERT INTO zhihu (name,headline,title,neirong) VALUES’ ‘(%s, %s, %s, %s)’

data_list = json_dict[‘data’]
for i in data_list:
# print(i)

# 用户名字
name = i['author']['name']
# print(name)

# 个性签名
headline = i['author']['headline']
# print(headline)

# 回答的话题
title = i['question']['title']
# print(title)

# 回答的内容
neirong = i['excerpt']
# print(neirong)

data = (name,headline,title,neirong)
print(data)
# print(type(data))
# break
# myhelper.execute_modify_sql(sql, data)

你可能感兴趣的:(知乎爬取)