urllib 从 php link 下载 excel 文件
urllib download excel file from php link
我正在尝试使用 urllib.urlretrieve (python 2.7) 从 url 下载 xls 文件列表。我能够获取该文件,但是文件顶部有一个 <script>
标记,使其在 excel 中无法读取。
这是我的:
import urllib
files= ['a','b', 'c', 'd', 'e', 'f']
url = 'http://www.thewebsite.com/data/dl_xls.php?bid='
for f in files:
urllib.urlretrieve(url + f, f + '.xls')
这将下载一个顶部包含以下内容的 xls 文件:
<script>parent.parent.location.href = '../../../../a';</script>
这使得它在 excel 中不可读。
如果我从 xls 中删除该脚本标签,文件将在 excel 中正确打开。
编辑 - 这是我来自 pypypy 的解决方案:
import urllib
files= ['a','b', 'c', 'd', 'e', 'f']
url = 'http://www.thewebsite.com/data/dl_xls.php?bid='
for f in files:
input_xls = f + '_in.xls'
urllib.urlretrieve(url + f, input_xls)
output = open(f + '_out.xls', "wb")
with open(input_xls, "rb") as i:
output.write(re.sub('<script>.*</script>', "", i.read(), re.I))
i.close()
output.close()
尝试构建一个正则表达式来匹配脚本标签并将其删除,即
import re
re.sub('<script>.*</script>', "", content, re.I)
这会将内容中的任何脚本标签替换为“”。
我正在尝试使用 urllib.urlretrieve (python 2.7) 从 url 下载 xls 文件列表。我能够获取该文件,但是文件顶部有一个 <script>
标记,使其在 excel 中无法读取。
这是我的:
import urllib
files= ['a','b', 'c', 'd', 'e', 'f']
url = 'http://www.thewebsite.com/data/dl_xls.php?bid='
for f in files:
urllib.urlretrieve(url + f, f + '.xls')
这将下载一个顶部包含以下内容的 xls 文件:
<script>parent.parent.location.href = '../../../../a';</script>
这使得它在 excel 中不可读。
如果我从 xls 中删除该脚本标签,文件将在 excel 中正确打开。
编辑 - 这是我来自 pypypy 的解决方案:
import urllib
files= ['a','b', 'c', 'd', 'e', 'f']
url = 'http://www.thewebsite.com/data/dl_xls.php?bid='
for f in files:
input_xls = f + '_in.xls'
urllib.urlretrieve(url + f, input_xls)
output = open(f + '_out.xls', "wb")
with open(input_xls, "rb") as i:
output.write(re.sub('<script>.*</script>', "", i.read(), re.I))
i.close()
output.close()
尝试构建一个正则表达式来匹配脚本标签并将其删除,即
import re
re.sub('<script>.*</script>', "", content, re.I)
这会将内容中的任何脚本标签替换为“”。