知乎ajax。。。

上代码这里写代码片

import requests
import json
import pymysql
db = pymysql.connect(host='127.0.0.1', user='root', password='123456', port=3306, database='xueqiu')
cursor = db.cursor()
for i in range(20,80,20):
    url = 'https://www.zhihu.com/api/v4/members/zhi-mao-44/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='+str(i)+'&limit=20&sort_by=created'
    headers = {
    'referer':'https://www.zhihu.com/people/zhi-mao-44/answers',
    'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36'
    }
    response = requests.get(url,headers=headers)
    html = response.text
    html = json.loads(html)
    # print(html)
    for i in html['data']:
        qianming=(i['author']['headline'])
        name=(i['author']['name'])
        answer=(i['excerpt'])
        question=(i['question']['title'])
        sql = "insert into zhihu(qianming,name,answer,question) values('" + qianming + "','" + name + "','" + answer + "','" + question + "')"
        cursor.execute(sql)
        db.commit()
cursor.close()
db.close()

你可能感兴趣的:(Python)