zoukankan      html  css  js  c++  java
  • xpath爬取网页评论,网址的的调用方法,数据库特殊字符的替换

    # -*- coding:utf-8-*-
    from lxml import etree
    import urllib
    import json
    import requests

    import MySQLdb
    id=0
    class SQL(object):
    conn=MySQLdb.connect(host="localhost",
    port=3306,
    user="root",
    passwd="123456",
    db="test",
    charset="utf8",)

    def insert(self,name,time,content): #函数的调用
    cur=self.conn.cursor()
    global id
    id+=1
    #cur.execute("create table pinglun (id int ,name text,time text,content text)")

    cur.execute("insert into pinglun VALUES ('%s' ,'%s','%s','%s')"%(id,name,time,content))
    cur.close()
    self.conn.commit()

    mysql=SQL()#实例化类
    def spider(url):
    html=requests.get(url)
    seletor=etree.HTML(html.text)
    content_field=seletor.xpath('//div[@class="l_post j_l_post l_post_bright "]') #空格不能省略
    #print type(content_field)
    #fd=open(u'评论'+'.txt','wb')
    for each in content_field:
    reply_info=json.loads(each.xpath('@data-field')[0])
    author=reply_info['author']['user_name']



    time=reply_info['content']['date']
    #print time
    content=each.xpath('div[@class="d_post_content_main"]/div/cc/div[@class="d_post_content j_d_post_content clearfix"]/text()')[0]
    contents=content.replace("'","")#'对于数据库来说是特殊符号,要用替换的方法
    mysql.insert(author,time,contents)


    #fd.write(author+' '+time+' '+content+' ')
    #fd.close()
    if __name__ == '__main__':
    for i in range(1,100):
    url="http://tieba.baidu.com/p/3522395718?pn=%s"%i
    spider(url)#网址的调用方法
    mysql.conn.close()

  • 相关阅读:
    避开unity的坑(转摘)
    独立游戏开发者创建工作室必看建议(转摘)
    程序员常去的网站
    使用Unity3D的50个技巧
    day 51 html 学习 js 学习
    day 50 Java Script 学习
    day 49 html 学习 css 学习
    day 47 html 学习 css 学习
    day 46 html 学习 列 表格,
    day45 html 初识,常见命令
  • 原文地址:https://www.cnblogs.com/ZHANG576433951/p/6151290.html
Copyright © 2011-2022 走看看