如何在python中使用来自http url的原始数据下载ms word docx文件

时间:2018-02-15 05:19:45

标签: python web-scraping

如果在浏览器中点击了以下网址,则会下载docx文件,我想用python自动下载。

https://hudoc.echr.coe.int/app/conversion/docx/?library=ECHR&id=001-176931&filename=CASE OF NDIDI v. THE UNITED KINGDOM.docx&logEvent=False

我试过以下

from docx import Document
import requests
import json
from bs4 import BeautifulSoup
dwnurl = 'https://hudoc.echr.coe.int/app/conversion/docx/?library=ECHR&id=001-176931&filename=CASE%20OF%20NDIDI%20v.%20THE%20UNITED%20KINGDOM.docx&logEvent=False'
doc = requests.get(dwnurl)

print(doc.content) #printing the document like b'PK\x03\x04\x14\x00\x06\x00\x08\x00\x00\x00!\x00!\xfb\x16\x01\x16\x02\x00\x00\xec\x0c\x00\x00\x13\x00\xc4\x01[Content_Types].xml \xa2\xc0\

print(doc.raw)  #printing the document like <urllib3.response.HTTPResponse object at 0x063D8BD0>

document = Document(doc.content)
document.save('test.docx')

#on document.save i have facing these issues

Traceback (most recent call last): File "scraping_hudoc.py", line 40, in <module> document = Document(doc.content) File "C:\Users\204387\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\api.py", line 25, in Document document_part = Package.open(docx).main_document_part File "C:\Users\204387\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\opc\package.py", line 116, in open pkg_reader = PackageReader.from_file(pkg_file) File "C:\Users\204387\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\opc\pkgreader.py", line 32, in from_file phys_reader = PhysPkgReader(pkg_file) File "C:\Users\204387\AppData\Local\Programs\Python\Python36-32\lib\site-packages\docx\opc\phys_pkg.py", line 101, in __init__ self._zipf = ZipFile(pkg_file, 'r') File "C:\Users\204387\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1108, in __init__ self._RealGetContents() File "C:\Users\204387\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1171, in _RealGetContents endrec = _EndRecData(fp) File "C:\Users\204387\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 241, in _EndRecData fpin.seek(0, 2) AttributeError: 'bytes' object has no attribute 'seek'

1 个答案:

答案 0 :(得分:2)

我已通过此

保存了ms word docx文件
import requests
def save_link(book_link, book_name):
    the_book = requests.get(book_link, stream=True)
    with open(book_name, 'wb') as f:
      for chunk in the_book.iter_content(1024 * 1024 * 2):  # 2 MB chunks
        f.write(chunk)

save_link("https://hudoc.echr.coe.int/app/conversion/docx/?library=ECHR&id=001-176931&filename=CASE%20OF%20NDIDI%20v.%20THE%20UNITED%20KINGDOM.docx&logEvent=False","CASE OF NDIDI v. THE UNITED KINGDOM.docx")