python爬虫——对包含客户信息源代码检索

需求场景:需要找到源码中指定的某些包含客户信息的字段。
版本1: 检索一个关键字,包含的则输出到控制台。

import os

rootDir = os.getcwd()

def scan_file(filename, dirname):

    if("hello" in filename):
        if("src" in dirname):
            print(os.path.join(dirname,filename))
    else:
        with open(os.path.join(dirname,filename)) as f:
            lines = f.readlines()
            for l in lines:
                #print(l)
                if("hello" in l):
                    if("/src" in dirname):
                        print(os.path.join(dirname,filename))
                    break

for dirName, subdirList, fileList in os.walk(rootDir):
    for fname in fileList:
        scan_file(fname, dirName)

版本2:检索多个关键字,输出包含关键字的文件与包含的关键字

rootDir = os.getcwd()
keywords = ["hello","world","thanks"]

def scan_file(filename, dirname,keyword):

   if(keyword in filename):
       if("/src" in dirname):
           return True
   else:
       with open(os.path.join(dirname,filename)) as f:
           lines = f.readlines()
           for l in lines:
               if(keyword in l):
                   if("/src" in dirname):
                       return True                 

for dirName, subdirList, fileList in os.walk(rootDir):
   for fname in fileList:
       flag = False
       for keyword in keywords:   
           if(scan_file(fname, dirName,keyword)):
               if(flag is False):
                      flag = True
               f = open('test.txt', 'a')
               f.write(keyword)
               f.write(" ,")   
               f.close()
       if(flag is True):
           f = open('test.txt', 'a')
           f.write("\n"+os.path.join(dirName,fname)+"\n") 
           f.close()

这个版本实现了基本功能,但是仍然不够完美。迭代的空间:

1.算法的性能,包括时间复杂度,代码的冗余、优雅
2.输出结果的可读性,最好能够按照模块对文件进行整理,呈现在excel中
3.细节:对png等不符合需求的文件进行排除。

留待读者思考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 Python 爬虫实现,用于从北京租房信息网站上爬取租房信息并将其导入到 Excel 文件中。 首先,我们需要安装以下 Python 库: - requests:用于发送 HTTP 请求和获取网页内容。 - Beautiful Soup:用于解析 HTML 和 XML 网页内容。 - openpyxl:用于操作 Excel 文件。 可以使用 pip 命令进行安装: ``` pip install requests beautifulsoup4 openpyxl ``` 接下来,我们需要编写 Python 代码: ```python import requests from bs4 import BeautifulSoup from openpyxl import Workbook # 发送 HTTP 请求并获取网页内容 url = 'https://bj.zu.anjuke.com/fangyuan/p1/' response = requests.get(url) html = response.text # 使用 Beautiful Soup 解析网页内容 soup = BeautifulSoup(html, 'html.parser') houses = soup.select('.zu-itemmod') # 创建 Excel 文件并添加表头 wb = Workbook() ws = wb.active ws.append(['标题', '链接', '小区', '面积', '租金']) # 遍历租房信息并将其添加到 Excel 文件中 for house in houses: title = house.select('.zu-info h3 a')[0].text.strip() link = house.select('.zu-info h3 a')[0]['href'] community = house.select('.details-item')[0].text.strip() area = house.select('.details-item')[1].text.strip() price = house.select('.zu-side strong')[0].text.strip() ws.append([title, link, community, area, price]) # 保存 Excel 文件 wb.save('beijing_rent.xlsx') ``` 该爬虫程序将会从北京租房信息网站的第一页开始爬取租房信息,包括标题、链接、小区、面积和租金,并将其添加到 Excel 文件中。你可以根据需要修改代码以实现更多功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半旧518

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值