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

贴在这里,引以为戒吧。

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 18 21:41:34 2018
@author: brave-man
blog: http://www.cnblogs.com/zrmw/
""" import requests
from bs4 import BeautifulSoup
import json
from threading import Thread

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

# 此函数用来获取上市公司的股票代码
def getCode():
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0"}
res = requests.get("http://www.cninfo.com.cn/cninfo-new/information/companylist", headers = headers)
res.encoding = "gb1232"
soup = BeautifulSoup(res.text, "html.parser")
# print(soup.select(".company-list"))
L = []
l1 = []
l2 = []
l3 = []
l4 = []
for i in soup.select(".company-list")[0].find_all("a"):
code = i.text[:6]
l1.append(code)
for i in soup.select(".company-list")[1].find_all("a"):
code = i.text[:6]
l2.append(code)
for i in soup.select(".company-list")[2].find_all("a"):
code = i.text[:6]
l3.append(code)
for i in soup.select(".company-list")[3].find_all("a"):
code = i.text[:6]
l4.append(code)
L = [l1, l2, l3, l4]
print(L[0])
return getAll(L) def getAll(L):
def t1(L):
for i in L[0]:
url_sszb = "http://www.cninfo.com.cn/information/brief/szmb{}.html".format(i)
getDetails(url_sszb)
def t2(L):
for i in L[1]:
url_zxqyb = "http://www.cninfo.com.cn/information/brief/szsme{}.html".format(i)
getDetails(url_zxqyb)
def t3(L):
for i in L[2]:
url_cyb = "http://www.cninfo.com.cn/information/brief/szcn{}.html".format(i)
getDetails(url_cyb)
def t4(L):
for i in L[3]:
url_hszb = "http://www.cninfo.com.cn/information/brief/shmb{}.html".format(i)
getDetails(url_hszb)
# tt1 = Thread(target = t1, args = (L, ))
# tt2 = Thread(target = t2, args = (L, ))
# tt3 = Thread(target = t3, args = (L, ))
# tt4 = Thread(target = t4, args = (L, ))
#
# tt1.start()
# tt2.start()
# tt3.start()
# tt4.start()
#
# tt1.join()
# tt2.join()
# tt3.join()
# tt4.join()
t1(L)
t2(L)
t3(L)
t4(L) if __name__ == "__main__":
getCode()

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

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

最新文章

  1. jackson中JSON字符串节点遍历和修改
  2. 收集数据至泛型Dictionary
  3. EJB3 QL查询
  4. 【转】bShare分享插件的使用
  5. 开机使用root用户登录
  6. Codeforces Round #303 (Div. 2) B 水 贪心
  7. phpcms v9和discuz X3.1实现同步登陆退出论坛(已实现)
  8. @property中有哪些属性关键字?/ @property 后面可以有哪些修饰符?
  9. Cocos2d-x V2.x -- 开发进阶和高级实例教程(一) 转
  10. java类初始化,使用构造方法
  11. kvm之四:从网上镜像安装虚拟机Centos6.8
  12. 理解Python协程:从yield/send到yield from再到async/await
  13. ExcelPackage 读取、导出excel
  14. Your branch and remoteBranchName have diverged solution
  15. Android 经验之文件下载
  16. SQL 语言类型
  17. sublime出现 unable download.......
  18. (2.15)Mysql之SQL基础——开发设计最佳规范
  19. PHP-流的概念与详细用法
  20. HttpSession 和URLRewriting

热门文章

  1. go等待N个线程完成操作总结
  2. 手把手在Ubuntu上面安装Docker
  3. Jenkins入门之执行定时任务
  4. LeetCode-63. 不同路径 II
  5. 行为型---状态者模式(State Pattern)
  6. composer Content-Length mismatch
  7. FastJson序列化Json自定义返回字段,普通类从spring容器中获取bean
  8. 【Java每日一题】20170215
  9. Log4j的扩展RollingFileAppender、DailyRollingFileAppender
  10. 几点建议帮你写出简洁的JS代码