搜狐新闻小爬虫,解析数据接口并插入mysql数据库( 二 )

html = requests.get(url,headers= headers).text#获取内容etree.HTML(str(html)).xpath(“”//article[@class = 'article']//p//text()')

  • 获取数据之后将其组装成sql语句,以便直接插入到数据库中 。
sql = " sql_insert = 'insert into information (`type`, url,author,title,content,postTime,addtime,`unique`) values (%s,%s,%s,%s,%s,%s,%s,%s)'"
  • 通过pymysql获取mysql的连接信息执行sql语句
conn = pymysql.connect(host=“xxxxxxx”, port=3306, user="xxxx", passwd="xxxxxx", db="news", charset="utf8")cursor = conn.cursor()cursor.execute(sql_insert)完整代码请转我的csdn连接!




推荐阅读