首页 / PYTHON / python爬取网业信息案例
python爬取网业信息案例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python爬取网业信息案例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3434字,纯文字阅读大概需要5分钟。
内容图文
![python爬取网业信息案例](/upload/InfoBanner/zyjiaocheng/1329/0cf1ce4c920b418390dbb7cc5ee5da66.jpg)
需求:爬取网站上的公司信息
代码如下:
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)
原文:https://www.cnblogs.com/qmfsun/p/11322893.html
内容总结
以上是互联网集市为您收集整理的python爬取网业信息案例全部内容,希望文章能够帮你解决python爬取网业信息案例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】