python 爬虫 requests+BeautifulSoup 爬取巨潮资讯公司概况代码实例
第一次写一个算是比较完整的爬虫,自我感觉极差啊,代码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 + 浏览器 的爬取巨潮资讯的方法代码。晚安~
python 爬虫 requests+BeautifulSoup 爬取巨潮资讯公司概况代码实例的更多相关文章
- 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】
[写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...
- [原创]python爬虫之BeautifulSoup,爬取网页上所有图片标题并存储到本地文件
from bs4 import BeautifulSoup import requests import re import os r = requests.get("https://re. ...
- Python 爬虫入门之爬取妹子图
Python 爬虫入门之爬取妹子图 来源:李英杰 链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果 ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- python爬虫-基础入门-爬取整个网站《3》
python爬虫-基础入门-爬取整个网站<3> 描述: 前两章粗略的讲述了python2.python3爬取整个网站,这章节简单的记录一下python2.python3的区别 python ...
- python爬虫-基础入门-爬取整个网站《2》
python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...
- python爬虫-基础入门-爬取整个网站《1》
python爬虫-基础入门-爬取整个网站<1> 描述: 使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数 ...
- Python爬虫教程-17-ajax爬取实例(豆瓣电影)
Python爬虫教程-17-ajax爬取实例(豆瓣电影) ajax: 简单的说,就是一段js代码,通过这段代码,可以让页面发送异步的请求,或者向服务器发送一个东西,即和服务器进行交互 对于ajax: ...
- Python爬虫实战之爬取百度贴吧帖子
大家好,上次我们实验了爬取了糗事百科的段子,那么这次我们来尝试一下爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 本篇目标 对百度贴吧的任意帖子进行抓取 指定是否只抓取楼主发帖 ...
随机推荐
- [机器学习] --- Getting Started With MachineLearning
一. What's machine learning Machine Learning is the science of gettingcomputers to act without being ...
- Maven三种仓库的配置
转自:https://www.cnblogs.com/jack1995/p/6925879.html Maven三种仓库的配置 1 本地仓库的配置 在第一篇中我们介绍过,Maven的仓库有三类,这里不 ...
- 教你用Python创建瀑布图
介绍 对于绘制某些类型的数据来说,瀑布图是一种十分有用的工具.不足为奇的是,我们可以使用Pandas和matplotlib创建一个可重复的瀑布图. 在往下进行之前,我想先告诉大家我指代的是哪种类型的图 ...
- 第8章 CentOS包管理详解
8.1 Linux上构建C程序的过程 在说明包相关的内容之前,我觉得有必要说一下在Linux上构建一个C程序的过程.我个人并没有学习过C,内容总结自网上,所以可能显得很小白,而且也并非一定正确,只希望 ...
- μC/OS-II 中的任务管理
1. 任务的状态及其转换 睡眠状态: 任务在没有被配备任务控制块或被剥夺了任务控制块时的状态叫做任务的睡眠状态. 等待状态: 正在运行的任务,需要等待一段时间或需要等待一个事件发生再运行时,该任务就会 ...
- nginx实现动态/静态文件缓存(week4_day1_part2)-技术流ken
nginx实现静态文件缓存实战 1.nginx静态文件缓存 如果要熟练使用nginx来实现文件的缓存,那下面的几个指令你必须要牢记于心 指令1:proxy_cache_path 作用:设置缓存数据的相 ...
- Django 系列博客(四)
Django 系列博客(四) 前言 本篇博客介绍 django 如何和数据库进行交互并且通过 model 进行数据的增删查改 ORM简介 ORM全称是:Object Relational Mappin ...
- MONGODB(二)——索引操作
一.1.插入10w条数据> for(var i = 0;i<100000;i++){... var rand = parseInt(i*Math.random());... db.pers ...
- [译]如何在.NET Core中使用System.Drawing?
你大概知道System.Drawing,它是一个执行图形相关任务的流行的API,同时它也不属于.NET Core的一部分.最初是把.NET Core作为云端框架设计的,它不包含非云端相关API.另一方 ...
- 【Java每日一题】20170302
20170301问题解析请点击今日问题下方的“[Java每日一题]20170302”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...