python request下载word,下载word文档python

本文介绍了一个Python爬虫程序,使用requests库从指定网址抓取并下载Word文档。程序首先使用BeautifulSoup解析网页,找到文档链接,然后通过requests.get()下载文件。遇到错误时,程序会继续执行,确保其他文件的下载。
摘要由CSDN通过智能技术生成

对于我的课程,我必须建立一个网络刮板,它可以浏览img,word文档和pdf的网站并将它们下载到一个文件中,我有img的下载工作,但当我将代码改为下载docs或pdf时,它根本找不到任何东西,我使用beautifulsoup来刮网站,我知道网站上有些文档和pdf文件无法下载。

from bs4 import BeautifulSoup

import urllib.request

import shutil

import requests

from urllib.parse import urljoin

import sys

import time

import os

import url

import hashlib

import re

url = 'http://www.soc.napier.ac.uk/~40009856/CW/'

path=('c:\\temp\\')

def ensure_dir(path):

directory = os.path.dirname(path)

if not os.path.exists(path):

os.makedirs(directory)

return path

os.chdir(ensure_dir(path))

def webget(url):

response = requests.get(url)

html = response.content

return html

def get_docs(url):

soup = make_soup(url)

docutments = [docs for d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Google Drive API和Python来实现将Word文档转换为Google Docs在线文档格式的功能。以下是基本的步骤: 1. 创建一个Google Cloud Platform账户并启用Google Drive API。 2. 安装Google API客户端库,可以使用pip来安装:`pip install --upgrade google-api-python-client`. 3. 在Google Cloud Platform控制台中创建一个OAuth 2.0客户端ID,并下载客户端密钥JSON文件。 4. 使用OAuth 2.0客户端ID和密钥JSON文件进行身份验证。 5. 使用Google Drive API上传Word文档文件。 6. 将上传的Word文档文件转换为Google Docs在线文档格式。 以下是一个简单的Python代码示例,用于将Word文档转换为Google Docs在线文档格式: ```python import os import io from google.oauth2 import service_account from googleapiclient.discovery import build from googleapiclient.errors import HttpError from googleapiclient.http import MediaFileUpload, MediaIoBaseUpload # 客户端密钥JSON文件的路径 CLIENT_SECRET_FILE = 'client_secret.json' # 要上传的Word文档文件的路径 WORD_DOCUMENT_FILE = 'example.docx' # 身份验证 SCOPES = ['https://www.googleapis.com/auth/drive'] creds = None if os.path.exists('token.json'): creds = service_account.Credentials.from_service_account_file(CLIENT_SECRET_FILE, scopes=SCOPES) else: flow = service_account.Credentials.from_service_account_file(CLIENT_SECRET_FILE, scopes=SCOPES) creds = flow.run_local_server(port=0) # 创建Drive API客户端 drive_service = build('drive', 'v3', credentials=creds) # 上传Word文档文件 file_metadata = {'name': 'example.docx'} media = MediaFileUpload(WORD_DOCUMENT_FILE, mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document') file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute() print('File ID: %s' % file.get('id')) # 将上传的Word文档文件转换为Google Docs在线文档格式 try: # 获取文件的ID file_id = file.get('id') # 创建Google Docs在线文档格式的文件 body = {'name': 'example', 'parents': [file_id], 'mimeType': 'application/vnd.google-apps.document'} doc_file = drive_service.files().create(body=body).execute() doc_file_id = doc_file.get('id') # 将Word文档内容复制到Google Docs在线文档中 request = drive_service.files().export_media(fileId=file_id, mimeType='application/vnd.openxmlformats-officedocument.wordprocessingml.document') content = io.BytesIO() downloader = MediaIoBaseUpload(content, request) done = False while done is False: status, done = downloader.next_chunk() print('Download %d%%.' % int(status.progress() * 100)) content.seek(0) request = drive_service.files().update(fileId=doc_file_id, media_body=MediaIoBaseUpload(content, mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')) response = request.execute() print('File ID: %s' % response.get('id')) except HttpError as error: print('An error occurred: %s' % error) ``` 在上面的示例中,我们首先使用OAuth 2.0客户端ID和密钥JSON文件进行身份验证。然后,我们使用Drive API上传Word文档文件,并将其转换为Google Docs在线文档格式。最后,我们将Word文档内容复制到Google Docs在线文档中,并将其保存在Google Drive中。 注意:上面的示例代码仅用于演示目的,实际应用中需要根据具体情况进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值