美文网首页
2017.07.20

2017.07.20

作者: 铁拳宝宝爱芝麻 | 来源:发表于2017-07-21 11:10 被阅读0次

scrapy 爬虫,能够将知乎上的问题和答案爬取并入库

item, itemloader, mysqldb

使用 twisted 异步框架,MySQLdb 入库时,构造的sql语句
cursor.execute(insert_sql, params)的时候,不管参数是什么类型,都只能使用 %s,不然会报错

比如:
insert_sql = "insert into tb_name values(%s, %s, %s, %s)"
params = (pram1, pram2, pram3, pram4)
不管 params 元组中的数据元素是什么类型的数据,在 insert_sql 中都只能使用 %s,不然就会报出 sql 语句的错误

在文档中查找(http://mysql-python.sourceforge.net/MySQLdb.html#cursor-objects)

     c=db.cursor()
     max_price=5
     c.execute("""SELECT spam, eggs, sausage FROM breakfast
      WHERE price < %s""", (max_price,))

In this example, max_price=5 Why, then, use %s in the string? Because MySQLdb will convert it to a SQL literal value, which is the string '5'. When it's finished, the query will actually say, "...WHERE price < 5".

Why the tuple? Because the DB API requires you to pass in any parameters as a sequence. Due to the design of the parser, (max_price) is interpreted as using algebraic grouping and simply as max_price and not a tuple. Adding a comma, i.e. (max_price,) forces it to make a tuple.

The only other method you are very likely to use is when you have to do a multi-row insert:

  c.executemany(
  """INSERT INTO breakfast (name, spam, eggs, sausage, price)
  VALUES (%s, %s, %s, %s, %s)""",
  [
  ("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),
  ("Not So Much Spam Plate", 3, 2, 0, 3.95 ),
  ("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )
  ] )

Here we are inserting three rows of five values. Notice that there is a mix of types (strings, ints, floats) though we still only use %s. And also note that we only included format strings for one row. MySQLdb picks those out and duplicates them for each row.


知乎存在反爬虫策略,即每间隔一段时间就会对 User-Agent 进行检验,程序自动爬虫的时候,就会发现,后面的 http 请求的返回码都是 403,打开这个请求的连接,发现知乎需要你输入验证码通过后才能继续进入。

后续在处理这个爬虫策略的问题

相关文章

  • 2017.07.20

    从始至终,我不知道我失去了什么又拥有了什么。我仿佛有无尽的力量去面对生活中的种种,又仿佛是一团散沙面临迷失。 如果...

  • 2017.07.20

    今天没啥子图 找教室找了很久才找到 我们1.2.4是一个女黑人教 3.5是一个男白人教 至于他两是哪里来的 从表面...

  • 2017.07.20

    每年夏天最热的时候反而是我生活热情最高昂的时候,也是我最最不想学习、奋斗的时候,但喜欢的东西一直没有变,想要一个漂...

  • 2017.07.20

    都说3个女人一台戏,我们有4个女人,直接闹翻天! 先来个人物简介 斐斐仔:xxx银行,行政管理人员,结婚一年,性格...

  • 2017.07.20

    朋友指点,说我单缺一味毒药——没有把自己逼到那种份上。 我知道他说的是对的。总有一类人,在人类的长河中,四方通达八...

  • 2017.07.20

    如果我们现在变得这么难 那一定是什么地方出了点问题

  • 2017.07.20

  • 2017.07.20

    推荐 You Belong To Me 演唱CarlaBruni 一起裹着毯子读诗的冬日,夏季两点钟趴在阳台上吹着...

  • 2017.07.20

    十几岁的时候我们对这个世界只有浅显的认识 以为成绩考不好被批评 喜欢的人不喜欢自己 友情的背叛就是天大的哀伤 ...

  • 2017.07.20

    因为很想吃烧烤和油炸又不愿意出门去买 所以和妈咪一起自制了味道超棒的牙签肉 牙签肉之超简易做法: 将瘦肉切成薄长条...

网友评论

      本文标题:2017.07.20

      本文链接:https://www.haomeiwen.com/subject/fggskxtx.html