第一次写一个算是比较完整的爬虫,自我感觉极差啊,代码low,效率差,也没有保存到本地文件或者数据库,强行使用了一波多线程导致数据顺序发生了变化。。。

贴在这里,引以为戒吧。

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Jul 18 21:41:34 2018
  4. @author: brave-man
  5. blog: http://www.cnblogs.com/zrmw/
  6. """
  7.  
  8. import requests
  9. from bs4 import BeautifulSoup
  10. import json
  11. from threading import Thread

  12. # 获取上市公司的全称,英文名称,地址,法定代表人(也可以获取任何想要获取的公司信息)
  13. def getDetails(url):
  14. headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"}
  15. res = requests.get("{}".format(url), headers = headers)
  16. res.encoding = "GBK"
  17. soup = BeautifulSoup(res.text, "html.parser")
  18.  
  19. details = {"code": soup.select(".table")[0].td.text.lstrip("股票代码:")[:6],
  20. "Entire_Name": soup.select(".zx_data2")[0].text.strip("\r\n "),
  21. "English_Name": soup.select(".zx_data2")[1].text.strip("\r\n "),
  22. "Address": soup.select(".zx_data2")[2].text.strip("\r\n "),
  23. "Legal_Representative": soup.select(".zx_data2")[4].text.strip("\r\n ")}
  24. # 这里将details转换成json字符串格式用作后期存储处理
  25. jd = json.dumps(details)
  26. jd1 = json.loads(jd)
  27. print(jd1)

  28. # 此函数用来获取上市公司的股票代码
  29. def getCode():
  30. headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"}
  31. res = requests.get("http://www.cninfo.com.cn/cninfo-new/information/companylist", headers = headers)
  32. res.encoding = "gb1232"
  33. soup = BeautifulSoup(res.text, "html.parser")
  34. # print(soup.select(".company-list"))
  35. L = []
  36. l1 = []
  37. l2 = []
  38. l3 = []
  39. l4 = []
  40. for i in soup.select(".company-list")[0].find_all("a"):
  41. code = i.text[:6]
  42. l1.append(code)
  43. for i in soup.select(".company-list")[1].find_all("a"):
  44. code = i.text[:6]
  45. l2.append(code)
  46. for i in soup.select(".company-list")[2].find_all("a"):
  47. code = i.text[:6]
  48. l3.append(code)
  49. for i in soup.select(".company-list")[3].find_all("a"):
  50. code = i.text[:6]
  51. l4.append(code)
  52. L = [l1, l2, l3, l4]
  53. print(L[0])
  54. return getAll(L)
  55.  
  56. def getAll(L):
  57. def t1(L):
  58. for i in L[0]:
  59. url_sszb = "http://www.cninfo.com.cn/information/brief/szmb{}.html".format(i)
  60. getDetails(url_sszb)
  61. def t2(L):
  62. for i in L[1]:
  63. url_zxqyb = "http://www.cninfo.com.cn/information/brief/szsme{}.html".format(i)
  64. getDetails(url_zxqyb)
  65. def t3(L):
  66. for i in L[2]:
  67. url_cyb = "http://www.cninfo.com.cn/information/brief/szcn{}.html".format(i)
  68. getDetails(url_cyb)
  69. def t4(L):
  70. for i in L[3]:
  71. url_hszb = "http://www.cninfo.com.cn/information/brief/shmb{}.html".format(i)
  72. getDetails(url_hszb)
  73. # tt1 = Thread(target = t1, args = (L, ))
  74. # tt2 = Thread(target = t2, args = (L, ))
  75. # tt3 = Thread(target = t3, args = (L, ))
  76. # tt4 = Thread(target = t4, args = (L, ))
  77. #
  78. # tt1.start()
  79. # tt2.start()
  80. # tt3.start()
  81. # tt4.start()
  82. #
  83. # tt1.join()
  84. # tt2.join()
  85. # tt3.join()
  86. # tt4.join()
  87. t1(L)
  88. t2(L)
  89. t3(L)
  90. t4(L)
  91.  
  92. if __name__ == "__main__":
  93. getCode()

没有考虑实际生产中突发的状况,比如网速延迟卡顿等问题。

速度是真慢,有时间会分享给大家 selenium + 浏览器 的爬取巨潮资讯的方法代码。晚安~

python 爬虫 requests+BeautifulSoup 爬取巨潮资讯公司概况代码实例的更多相关文章

  1. 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】

    [写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...

  2. [原创]python爬虫之BeautifulSoup,爬取网页上所有图片标题并存储到本地文件

    from bs4 import BeautifulSoup import requests import re import os r = requests.get("https://re. ...

  3. Python 爬虫入门之爬取妹子图

    Python 爬虫入门之爬取妹子图 来源:李英杰  链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果 ...

  4. 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...

  5. python爬虫-基础入门-爬取整个网站《3》

    python爬虫-基础入门-爬取整个网站<3> 描述: 前两章粗略的讲述了python2.python3爬取整个网站,这章节简单的记录一下python2.python3的区别 python ...

  6. python爬虫-基础入门-爬取整个网站《2》

    python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...

  7. python爬虫-基础入门-爬取整个网站《1》

    python爬虫-基础入门-爬取整个网站<1> 描述: 使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数 ...

  8. Python爬虫教程-17-ajax爬取实例(豆瓣电影)

    Python爬虫教程-17-ajax爬取实例(豆瓣电影) ajax: 简单的说,就是一段js代码,通过这段代码,可以让页面发送异步的请求,或者向服务器发送一个东西,即和服务器进行交互 对于ajax: ...

  9. Python爬虫实战之爬取百度贴吧帖子

    大家好,上次我们实验了爬取了糗事百科的段子,那么这次我们来尝试一下爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 本篇目标 对百度贴吧的任意帖子进行抓取 指定是否只抓取楼主发帖 ...

随机推荐

  1. [机器学习] --- Getting Started With MachineLearning

    一. What's machine learning Machine Learning is the science of gettingcomputers to act without being  ...

  2. Maven三种仓库的配置

    转自:https://www.cnblogs.com/jack1995/p/6925879.html Maven三种仓库的配置 1 本地仓库的配置 在第一篇中我们介绍过,Maven的仓库有三类,这里不 ...

  3. 教你用Python创建瀑布图

    介绍 对于绘制某些类型的数据来说,瀑布图是一种十分有用的工具.不足为奇的是,我们可以使用Pandas和matplotlib创建一个可重复的瀑布图. 在往下进行之前,我想先告诉大家我指代的是哪种类型的图 ...

  4. 第8章 CentOS包管理详解

    8.1 Linux上构建C程序的过程 在说明包相关的内容之前,我觉得有必要说一下在Linux上构建一个C程序的过程.我个人并没有学习过C,内容总结自网上,所以可能显得很小白,而且也并非一定正确,只希望 ...

  5. μC/OS-II 中的任务管理

    1. 任务的状态及其转换 睡眠状态: 任务在没有被配备任务控制块或被剥夺了任务控制块时的状态叫做任务的睡眠状态. 等待状态: 正在运行的任务,需要等待一段时间或需要等待一个事件发生再运行时,该任务就会 ...

  6. nginx实现动态/静态文件缓存(week4_day1_part2)-技术流ken

    nginx实现静态文件缓存实战 1.nginx静态文件缓存 如果要熟练使用nginx来实现文件的缓存,那下面的几个指令你必须要牢记于心 指令1:proxy_cache_path 作用:设置缓存数据的相 ...

  7. Django 系列博客(四)

    Django 系列博客(四) 前言 本篇博客介绍 django 如何和数据库进行交互并且通过 model 进行数据的增删查改 ORM简介 ORM全称是:Object Relational Mappin ...

  8. MONGODB(二)——索引操作

    一.1.插入10w条数据> for(var i = 0;i<100000;i++){... var rand = parseInt(i*Math.random());... db.pers ...

  9. [译]如何在.NET Core中使用System.Drawing?

    你大概知道System.Drawing,它是一个执行图形相关任务的流行的API,同时它也不属于.NET Core的一部分.最初是把.NET Core作为云端框架设计的,它不包含非云端相关API.另一方 ...

  10. 【Java每日一题】20170302

    20170301问题解析请点击今日问题下方的“[Java每日一题]20170302”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...