第一次写一个算是比较完整的爬虫,自我感觉极差啊,代码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 爬取巨潮资讯公司概况代码实例的更多相关文章

  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. 流式大数据计算实践(1)----Hadoop单机模式

    一.前言 1.从今天开始进行流式大数据计算的实践之路,需要完成一个车辆实时热力图 2.技术选型:HBase作为数据仓库,Storm作为流式计算框架,ECharts作为热力图的展示 3.计划使用两台虚拟 ...

  2. 1.let命令总结

    1.let用法类似于var,但是let只在所在代码块有效 { let a = 10; var b = 1; } a // ReferenceError: a is not defined. b // ...

  3. 使用VSCode如何调试C#控制台程序_2_加深总结

    要想使用调试,必须创建项目 1-你要调式的类,控制台类等等,你需要放在一个项目下,这个项目最好是由使用.net core创建的,VSCode对应的命令为: dotnet new console(这里以 ...

  4. [转]windows 10 搭建angular开发环境

    本文转自:https://www.cnblogs.com/lilunpai/articles/7992538.html 一.环境介绍 1.开发环境:Windows10 2.开发ide工具:VS cod ...

  5. 第一册:lesson ninety-three。

    原文:  our new neighbor. Nigel is our new next-door neighbor. He is a pilot. He was in the R.A.F. He w ...

  6. IdentityServer4客户端JWT解密实现(基于.net4.0)

    情景:公司项目基于.net4.0,web客户端实现单点登录需要自己解密id_token,对于jwt解密,.net提供了IdentityModel类库,但是4.0中该类库不可用,所以自己实现了解密方法. ...

  7. Hangfire定时任务设置CronExpression表达式

    Cron format helper This utility helps you build Cron expressions easily by choosing job scheduling s ...

  8. FastReport.Net

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. 从零开始学安全(十三)●SQL server 2008 R2 安装

    安装过程1.下载并解压 sql_server_2008_r2_enterprise 点击 setup . 2.打开后如图,点击左侧的 安装 ,再点击右边的 全新安装或向现有安装添加功能. 3.安装支持 ...

  10. .net Core使用Orcle官方驱动连接数据库

    最近在研究.net Core,因为公司的项目用到的都是Oracle数据库,所以简单试一下.net Core怎样连接Oracle. Oracle官方现在已经提供.net Core的官方驱动(预览版),也 ...