【发布时间】:2017-03-23 20:40:13
【问题描述】:
我需要从下面的 html 页面中提取 csv 文件,一旦我得到它,我就可以用它做一些事情。下面是从先前的作业中提取特定的 html 代码行的代码。网址是'https://vincentarelbundock.github.io/Rdatasets/datasets.html' 那是测试代码,因此它在找到该行时会暂时中断。 我的 csv 行的一部分是 href 是 csv/datasets/co2.csv (我认为是 unicode 类型)
如何打开 co2.csv? 对问题的任何格式问题感到抱歉。代码已经被编辑器切片了。
import urllib
url = 'https://vincentarelbundock.github.io/Rdatasets/datasets.html'
from BeautifulSoup import *
def scrapper(url,k):
c=0
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
#. Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
y= (tag.get('href', None))
#print ((y))
if y == 'csv/datasets/co2.csv':
print y
break
c= c+ 1
if c is k:
return y
print(type(y))
for w in range(29):
print(scrapper(url,w))
【问题讨论】:
-
请改进您的问题:不清楚您是只想处理 single
co2.csv文件,还是要处理 all 链接在 html 页面上的 csv 文件。 -
我只想要一个文件。 Co2.csv
-
我想获取该文件并对其进行数据分析(线性回归)。
标签: python html csv parsing url