python3爬虫的一些技巧

为什么80%的码农都做不了架构师?>>>   hot3.png

页面内容编码问题

方式一

html = requests.get(url,headers = headers)
html.encoding = 'utf-8'
htmltext = html.text

方式二

html = requests.get(url,headers = headers).content
含图片等内容

并行化运行

from multiprocessing.dummy import Pool
pool = Pool(4)  #4是你计算机的内核数
result = pool.map(爬虫函数,对应链接)
pool.close()
pool.join()

参考资料: https://www.waitalone.cn/python-thread-map.html

http://blog.csdn.net/seetheworld518/article/details/49639651

数据库相关

import torndb
db = get_mysql_conn()
    db.execute("START TRANSACTION")
    for i in range(1000):
        ret = db.insertmany('insert数据)
    db.execute("COMMIT")

数据库优化:

  • 1、用事务提交
  • 2、查询数据多用索引,少用条件查询
  • 3、sql多用打包然后1次性提交或者查询处理
  • 4、在应用端添加逻辑来提高性能

数据库优化参考资料:

MySQL性能优化的最佳20+条经验

我的博文地址:http://www.nigaea.com/experience/93.html

转载于:https://my.oschina.net/at5/blog/817921

你可能感兴趣的:(python3爬虫的一些技巧)