需求:爬取网站上的公司信息

代码如下:

import json
import os
import shutil
import requests
import re
import time requests.packages.urllib3.disable_warnings() #通过url请求接口,获取返回数据
def getPage(url,headers):
try:
response = requests.get(url=url, headers=headers, verify=False)
response.encoding = 'utf-8'
if response.status_code == 200:
#print (response.text)
return response.text
else:
print('请求异常:{} status:{}'.format(url, response.status_code))
except Exception as e:
print('请求异常: {} error: {}'.format(url, e))
return None #删除文件的重复行
def file2uniq(file,destpath):
sum = 0
sum_pre = 0
addrs = set()
with open(file, 'r',encoding='utf8') as scan_file:
for line in scan_file.readlines():
sum_pre += 1
# addr = get_addr(line)
# line.decode('utf8')
addrs.add(line)
scan_file.close()
with open(destpath, 'w',encoding='utf8') as infile:
while len(addrs) > 0:
sum += 1
infile.write(addrs.pop())
infile.close() if (os.path.exists(file)):
os.remove(file)
try:
os.rename(destpath, file)
except Exception as e:
print (e)
print ('rename file fail\r')
else:
print ('rename file success\r')
#print(addrs)
print("去重之前文本条数: "+str(sum_pre))
print("去重之后文本条数: "+str(sum))
return sum_pre,sum #通过正则表达式提取页面内容
def parseHtml(html):
#pattern = re.compile(r'<tr> <td class="tx">.+\s(.+)', re.I) # 不区分大小写 匹配股票名称 # 不区分大小写 获取完整公司名
pattern = re.compile(r'<td class="text-center">.+</td> <td> <a href="/firm_.+">\s(.+)', re.I) # 获取证券公司
#pattern = re.compile(r'\t(.+)[\s]+</a> </td> <td class="text-center">.+</td> <td class="text-center">.+</td> </tr>', re.I)
#pattern = re.compile(r'\t(.+)\s\t\t\t\t\t\t\t </a> </td> <td class="text-center">.+</td> <td class="text-center">.+</td> </tr> <tr> <td class="tx">', re.I) # 不区分大小写
#pattern = re.compile(r'</a>\s</td>\s<td class="text-center">.+</td> <td> <a href="/firm_.+.html">\s(.+)[\s]+</a> </td> <td> <a href="/firm_.+.html">\s(.+)', re.I) # 不区分大小写 匹配股票名称 items = re.findall(pattern, html)
#print (items)
for item in items:
yield {
'orgName': item.strip(),
} def write2txt(content):
with open(file, 'a', encoding='utf-8') as f:
f.write(json.dumps(content, ensure_ascii=False) + '\n') def removeStr(old_str,new_str):
"""
with open('sanban.txt', 'a', encoding='utf-8') as fpr:
content = fpr.read()
content = content.replace(r'{"orgName": "', '')
content = content.replace(r'"}', '')
"""
file_data = ""
with open(file, 'r', encoding='utf-8') as f:
for line in f:
if old_str in line:
line = line.replace(old_str,new_str)
file_data += line
with open(file, 'w', encoding='utf-8') as f:
f.write(file_data) def main(page):
#url = 'https://www.qichacha.com/elib_sanban.html?p=' + str(page)
url = 'https://www.qichacha.com/elib_ipo.html?p=' + str(page) # https://www.qichacha.com/elib_ipo.html?p=2
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
}
print (url)
html = getPage(url,headers)
#print (html)
for item in parseHtml(html):
print(item)
write2txt(item)
removeStr(r'{"orgName": "','')
removeStr(r'"}', '') file2uniq(file, destpath) if __name__ == '__main__':
file = r'orgName.txt'
#file = r'midOrg.txt'
#sourcepath = r'sanban.txt'
destpath = r'temp.txt'
for page in range(1,2):
main(page)
time.sleep(1)

python爬取网业信息案例的更多相关文章

  1. Python爬取网易云音乐歌手歌曲和歌单

    仅供学习参考 Python爬取网易云音乐网易云音乐歌手歌曲和歌单,并下载到本地 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做 ...

  2. 用Python爬取网易云音乐热评

    用Python爬取网易云音乐热评 本文旨在记录Python爬虫实例:网易云热评下载 由于是从零开始,本文内容借鉴于各种网络资源,如有侵权请告知作者. 要看懂本文,需要具备一点点网络相关知识.不过没有关 ...

  3. Python爬取网易云热歌榜所有音乐及其热评

    获取特定歌曲热评: 首先,我们打开网易云网页版,击排行榜,然后点击左侧云音乐热歌榜,如图: 关于如何抓取指定的歌曲的热评,参考这篇文章,很详细,对小白很友好: 手把手教你用Python爬取网易云40万 ...

  4. Python 爬取网易云歌手的50首热门作品

    使用 requests 爬取网易云音乐 Python 代码: import json import os import time from bs4 import BeautifulSoup impor ...

  5. Python爬取网易云歌单

    目录 1. 关键点 2. 效果图 3. 源代码 1. 关键点 使用单线程爬取,未登录,爬取网易云歌单主要有三个关键点: url为https://music.163.com/discover/playl ...

  6. 爬虫实战(二) 用Python爬取网易云歌单

    最近,博主喜欢上了听歌,但是又苦于找不到好音乐,于是就打算到网易云的歌单中逛逛 本着 "用技术改变生活" 的想法,于是便想着写一个爬虫爬取网易云的歌单,并按播放量自动进行排序 这篇 ...

  7. Python爬取拉勾网招聘信息并写入Excel

    这个是我想爬取的链接:http://www.lagou.com/zhaopin/Python/?labelWords=label 页面显示如下: 在Chrome浏览器中审查元素,找到对应的链接: 然后 ...

  8. python爬取豆瓣视频信息代码

    目录 一:代码 二:结果如下(部分例子)   这里是爬取豆瓣视频信息,用pyquery库(jquery的python库). 一:代码 from urllib.request import quote ...

  9. python爬取网易云音乐歌曲评论信息

    网易云音乐是广大网友喜闻乐见的音乐平台,区别于别的音乐平台的最大特点,除了“它比我还懂我的音乐喜好”.“小清新的界面设计”就是它独有的评论区了——————各种故事汇,各种金句频出.我们可以透过歌曲的评 ...

随机推荐

  1. git中的SSL certificate problem: unable to get local issuer certificate错误的解决办法

    我们在使用git初始化一个项目时,尤其是通过git submodule update --init --remote初始化子模块时,可能会遇到下面这个错误: fatal: unable to acce ...

  2. Android框架式编程之架构方案

    目前移动端应用市场已经是百花齐放,很多独角兽公司都是通过App创业发展起来的,现在App类型更加丰富,功能越来越完善,基本上涵盖了各个行业的每个角落.为了开发出更加有竞争力的App,不仅需要功能上有创 ...

  3. 林克的小本本之——记一些基础的linux命令

    查看shell cat /etc/shells 查看系统支持的shell echo $SHELL 查看目前正在使用的shell 快捷键 Ctrl+a 跳到行首 Ctrl+e 跳到行尾 Ctrl+u 删 ...

  4. 关于 IIS Express 常用设置

    关于 IIS Express 常用设置 站点绑定 IIS Express Web 服务器默认只绑定了 localhost 的主机名,这就意味着无法通过内网或其他自定义域名进行访问,可通过如下操作添加其 ...

  5. Electron npm install 常见错误(Linux)

    Linux版本 Ubuntu 12.04 (32bit) 安装Git sudo apt-get install git 生成ssh key #查看有没有sshkey cd ~/.ssh #生成 ssh ...

  6. 分布式缓存 Redis 集群搭建

    Redis 集群简介 Redis Cluster 即 Redis 集群,是 Redis 官方在 3.0 版本推出的一套分布式存储方案.完全去中心化,由多个节点组成,所有节点彼此互联.Redis 客户端 ...

  7. 使用PowerShell实现服务器常用软件的无人值守安装

    操作系统:windows server 2016 , windows server 2019 软件环境: 类型 名称 版本   系统功能 TelnetClien       IIS   启用Asp.n ...

  8. 史上最全Oracle数据泵常用命令

    本文转自https://blog.csdn.net/Enmotech/article/details/102848825 墨墨导读:expdp和impdp是oracle数据库之间移动数据的工具,本文简 ...

  9. 百度BAE专业版申购SSL证书

    这几天开发了一个小程序,本来想放到BAE基础版的,但是基础版现在不能再新增项目了,想了一下,出点血,配了个专业版.但是专业版的SSL证书是需要配的,而小程序必须使用https,所有必须申请一个证书.在 ...

  10. Linux禁用root用户

    在创建各种云主机的时候,云服务商给的都是root用户,这很方便,但是有某些时候会造成一些困扰,日后在服务器上启动各种服务后,仅仅拥有root权限的用户才能访问更改这些服务,这样会造成一些不必要的困扰, ...