python实现多个网站站内链接批量导出

本例多个网站的网址保存在”D:\test.xlsx”文件中第一列,代码运行结束后第二列导出站内链接,链接名保存在第三列,以下代码已经在Python27中调试通过:

#coding=utf-8
import requests
import os

from bs4 import BeautifulSoup
from openpyxl import Workbook
from openpyxl import load_workbook
from requests import exceptions

def get_html(url):
    '''
    headers = {
        'User-Agent': 'Mozilla/5.0(Macintosh; Intel Mac OS X 10_11_4)\
    AppleWebKit/537.36(KHTML, like Gecko) Chrome/52 .0.2743. 116 Safari/537.36'
    } #模拟浏览器访问
    '''
    headers = {
        'User-Agent': 'Mozilla/5.0(Windows NT 10.0; WOW64; Trident/7.0; rv:11.0)\
    like Gecko'
    }  # 模拟浏览器访问
    t1=30
    try:
        r = requests.get(url,timeout=t1, headers = headers)       #请求访问网站
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        #print(r.status_code)
        if r.status_code == 200:
           return r.text
        else:
            return ''
    except requests.RequestException as e:
        print(e)
        #print(url)
        return ''

def rexcel(excelFile): #读excel文件
    list1 = []
    if not os.path.exists(excelFile):
        print("文件不存在")
        return list1
    wb = load_workbook(excelFile)
    ws = wb.active
    for j in range(ws.max_row):
           r = j + 1
           list1.append(ws.cell(row=r, column=1).value)
    return  list1

def wexcel(excelFile,hr1,st1): #写excel文件
    if not os.path.exists(excelFile):
        print("文件不存在")
        return 0
    wb = load_workbook(excelFile)
    ws = wb.active
    #print len(hr1)
    #print hr1[]
    for row in range(len(hr1)):
        #print(row)
        r = row + 1
        ws.cell(row=r, column=2).value =hr1[row]
        ws.cell(row=r, column=3).value =st1[row]
        if row>1048575:
            break
    wb.save(excelFile)
    return  1



if __name__ == '__main__': #程序入口
#site = 'http://www.hengripumps.com/'
 excelFile = 'D:/test.xlsx'
 list1=rexcel(excelFile)
 hr1 = []
 st1 = []
 for m in range(len(list1)):
    site = list1[m]
    #print(site)
    if site == None:
        break
    else:
        print site

    if site[0:4]=='http':
      demo=get_html(site)
      if demo == '':
          hr1.append(site)
          st1.append(u'获取主页数据失败')
          continue

      soup = BeautifulSoup(demo,"html.parser")

      if site[-1] == '/':
          site = site[0:-1]
      #print site
      #print demo
      i = 0
      for a in soup.find_all('a'):
          i += 1
          if a.has_attr('href'):
              if (a['href'][0:4]!='http') and ('@'not in a['href']) and ('javascript:'not in a['href']) and ('javasrcipt:'not in a['href']) and ('tel:'not in a['href']):  #非站外链接且非正常链接
                urlg=a['href']
                if len(urlg)>0:
                 if urlg[0]!='/':
                     urlg = '/'+urlg
                urlhb = site +urlg
                if urlhb in hr1:#过虑重复链接
                    continue
                hr1.append(urlhb)
                st1.append(a.string)
      if i == 0:
          hr1.append(site)
          st1.append(u'该网页无链接')
 wexcel(excelFile, hr1,st1)
 

运行后的”D:\test.xlsx”文件如下图:
在这里插入图片描述
补充说明:出现爬取网页报错提示状态码404,往往是execl表中网址后有不可见字符造成的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
批量清理多个WAV文件中的静音片段,可以使用Python中的文件操作和循环语句。以下是一种基于Python实现VAD技术,批量清理多个WAV文件中的静音片段的方法: 1. 导入所需的库:例如pydub, webrtcvad等。 2. 使用os库列出所有待处理的WAV文件。 3. 使用pydub库加载每个WAV文件。 4. 将每个WAV文件转换为PCM数据。 5. 使用webrtcvad库检测PCM数据中的活动和静默部分。 6. 根据检测结果,将静默部分从PCM数据中删除。 7. 将处理后的PCM数据保存为WAV文件。 以下是一个简单的Python代码示例,可以实现VAD技术批量清理多个WAV文件中的静音片段: ``` import os from pydub import AudioSegment import webrtcvad # 定义参数 frame_duration_ms = 30 frame_size = int(frame_duration_ms * 16 / 1000) vad = webrtcvad.Vad() vad.set_mode(3) # 列出所有待处理的WAV文件 wav_files = [f for f in os.listdir() if f.endswith('.wav')] # 循环处理每个WAV文件 for wav_file in wav_files: # 加载音频文件 audio = AudioSegment.from_file(wav_file) # 将音频转换为PCM数据 pcm_data = audio.raw_data # 检测静默部分 active = [] start = 0 audio_length = len(pcm_data) sample_rate = audio.frame_rate while start < audio_length: end = min(start + frame_size, audio_length) frame = pcm_data[start:end] if vad.is_speech(frame, sample_rate): active.extend(range(start, end)) start = end # 提取静默部分 segments = [] for k, g in groupby(enumerate(active), lambda i_x:i_x[0]-i_x[1]): segment = list(map(itemgetter(1), g)) start = segment[0] * 2 end = segment[-1] * 2 segments.append((start, end)) # 删除静默部分 for segment in segments: audio = audio[:segment[0]] + audio[segment[1]:] # 保存处理后的音频文件 audio.export("processed_" + wav_file, format="wav") ``` 这段代码使用了os库列出所有待处理的WAV文件,并使用循环语句依次处理每个WAV文件。请注意,要根据实际情况调整VAD检测器的参数以达到最佳效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值