python爬取网业信息案例
需求:爬取网站上的公司信息

代码如下:
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爬取网业信息案例的更多相关文章
- Python爬取网易云音乐歌手歌曲和歌单
仅供学习参考 Python爬取网易云音乐网易云音乐歌手歌曲和歌单,并下载到本地 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做 ...
- 用Python爬取网易云音乐热评
用Python爬取网易云音乐热评 本文旨在记录Python爬虫实例:网易云热评下载 由于是从零开始,本文内容借鉴于各种网络资源,如有侵权请告知作者. 要看懂本文,需要具备一点点网络相关知识.不过没有关 ...
- Python爬取网易云热歌榜所有音乐及其热评
获取特定歌曲热评: 首先,我们打开网易云网页版,击排行榜,然后点击左侧云音乐热歌榜,如图: 关于如何抓取指定的歌曲的热评,参考这篇文章,很详细,对小白很友好: 手把手教你用Python爬取网易云40万 ...
- Python 爬取网易云歌手的50首热门作品
使用 requests 爬取网易云音乐 Python 代码: import json import os import time from bs4 import BeautifulSoup impor ...
- Python爬取网易云歌单
目录 1. 关键点 2. 效果图 3. 源代码 1. 关键点 使用单线程爬取,未登录,爬取网易云歌单主要有三个关键点: url为https://music.163.com/discover/playl ...
- 爬虫实战(二) 用Python爬取网易云歌单
最近,博主喜欢上了听歌,但是又苦于找不到好音乐,于是就打算到网易云的歌单中逛逛 本着 "用技术改变生活" 的想法,于是便想着写一个爬虫爬取网易云的歌单,并按播放量自动进行排序 这篇 ...
- Python爬取拉勾网招聘信息并写入Excel
这个是我想爬取的链接:http://www.lagou.com/zhaopin/Python/?labelWords=label 页面显示如下: 在Chrome浏览器中审查元素,找到对应的链接: 然后 ...
- python爬取豆瓣视频信息代码
目录 一:代码 二:结果如下(部分例子) 这里是爬取豆瓣视频信息,用pyquery库(jquery的python库). 一:代码 from urllib.request import quote ...
- python爬取网易云音乐歌曲评论信息
网易云音乐是广大网友喜闻乐见的音乐平台,区别于别的音乐平台的最大特点,除了“它比我还懂我的音乐喜好”.“小清新的界面设计”就是它独有的评论区了——————各种故事汇,各种金句频出.我们可以透过歌曲的评 ...
随机推荐
- Rancher 2.3.3发布!默认支持K8S 1.16
2019年11月28日,Rancher Labs发布了Rancher全新版本2.3.3,该版本默认支持Kubernetes1.16,此外还带来了其他功能与优化. 目前,Rancher的Latest和S ...
- 一起学Vue之计算属性和侦听器
概述 在Vue开发中,模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.当你想要在模板中多次引用相同表达式时,就会更加难以处理.所以,对于任何复 ...
- Yii2 框架跑脚本时内存泄漏问题分析
现象 在跑 edu_ocr_img 表的归档时,每跑几万个数据,都会报一次内存耗尽 PHP Fatal error: Allowed memory size of 134217728 bytesex ...
- Elasticsearch7.5.0源码编译
环境及工具 JDK12 Gradle5.6.2 GIT 源码及预处理 到github将代码clone下来,可以根据自己的需求来获取版本,例如 git checkout v7.5.0 提前下载gradl ...
- 【zabbix告警监控】配置zabbix监控nginx服务
zabbix监控nginx,nginx需要添加--with-http_stub_status模块 使用zabbix监控nginx,首先nginx需要配置开启ngx_status.但是我这边nginx安 ...
- Spring注解之@Autowired
前言 说起Spring的@Autowired注解,想必大家已经熟悉的不能再熟悉了.本文就针对此最常用的注解,梳理一下它的功能和原理,争取从源码的角度将此注解讲通,如有写的不准确的地方,欢迎各位园友拍砖 ...
- Python用pip安装第三方库时换源下载
pip默认是从Python官网下载第三方库,从国外下载当然不如从国内下载来得快 豆瓣:https://pypi.doubanio.com/simple 还有其它源,阿里云等等,一个就够用了 用pip安 ...
- DevExpress Skins<2>
Skins DevExpress WinForms控件包括许多现成的绘画主题(或外观),用于更改DevExpress表单和控件的外观.您可以通过运行WinForms演示应用程序并使用外观选择器在可用主 ...
- 升鲜宝V2.0_杭州生鲜配送行业,升鲜宝供应链B端订货系统使用说明_升鲜宝生鲜供应链管理系统_15382353715_余东升
升鲜宝V2.0_杭州生鲜配送行业,升鲜宝供应链B端订货系统使用说明_升鲜宝生鲜供应链管理系统_15382353715 升鲜宝生鲜配送供应链系统经过这些年的发展,形成一套独特的订 ...
- 项目中遇到的问题:IDEA maven项目报错:程序包com.sun.image.codec.jpeg不存在
错误截图: 解决方法:在pom.xml文件中间加上以下代码: 代码: <plugin> <groupId>org.apache.maven.plugins</groupI ...