2017-05-25 173 views
0

我需要从(https://www.sec.gov/litigation/suspensions.shtml)给定网站下载所有文件。它有从1995年到2017年的数据,每年里面有多个需要下载的文件的链接。 Th文件使用.pdf,.htm和.txt格式。我试图通过查看各种教程来抓取数据,但是我需要做的与通常的网页抓取教程不同。我使用了下面的代码,但它并没有达到我的目的。我是python的新手,我在这里被困在如何前进的道路上。任何人都可以请建议需要做什么。使用python从网站下载文件

import requests 
from bs4 import BeautifulSoup 

r = requests.get("https://www.sec.gov/litigation/suspensions.shtml") 
r.content 

soup = BeautifulSoup(r.content) 
print soup.prettify() 

links = soup.find_all("a") 

for link in links: 
     print "<a href= '%s'>%s</a>" %(link.get("href"), link.text) 

g_data = soup.find_all("p", {"id": "archive-links"}) 
print g_data 

for item in g_data: 
    print item.text 
+0

什么是你的脚本 – mtkilic

+0

快速和肮脏的方式输出:只是'grep的-o'像https://www.sec.gov/litigation/suspensions/2017/34-80766-所有URL o.pdf,并使用'wget'将它们全部下载 – zyxue

+0

@mtkilic - 嗨,使用Denis的代码后,我得到的输出为“Got links:set([])”。我无法下载这些文件。你能帮我弄清楚是什么问题吗? –

回答

0

这应该做的工作。检查了Python 3.6,但代码应该是Python2.7兼容的。 主要想法是找到每年的链接,然后抓取每年的pdf,htm和txt文件的所有链接。

from __future__ import print_function 

import requests 
from bs4 import BeautifulSoup 


def file_links_filter(tag): 
    """ 
    Tags filter. Return True for links that ends with 'pdf', 'htm' or 'txt' 
    """ 
    if isinstance(tag, str): 
     return tag.endswith('pdf') or tag.endswith('htm') or tag.endswith('txt') 


def get_links(tags_list): 
    return [WEB_ROOT + tag.attrs['href'] for tag in tags_list] 


def download_file(file_link, folder): 
    file = requests.get(file_link).content 
    name = file_link.split('/')[-1] 
    save_path = folder + name 

    print("Saving file:", save_path) 
    with open(save_path, 'wb') as fp: 
     fp.write(file) 


WEB_ROOT = 'https://www.sec.gov' 
SAVE_FOLDER = '~/download_files/' # directory in which files will be downloaded 

r = requests.get("https://www.sec.gov/litigation/suspensions.shtml") 

soup = BeautifulSoup(r.content, 'html.parser') 

years = soup.select("p#archive-links > a") # css selector for all <a> inside <p id='archive'> tag 
years_links = get_links(years) 

links_to_download = [] 
for year_link in years_links: 
    page = requests.get(year_link) 
    beautiful_page = BeautifulSoup(page.content, 'html.parser') 

    links = beautiful_page.find_all("a", href=file_links_filter) 
    links = get_links(links) 

    links_to_download.extend(links) 

# make set to exclude duplicate links 
links_to_download = set(links_to_download) 

print("Got links:", links_to_download) 

for link in set(links_to_download): 
    download_file(link, SAVE_FOLDER) 
+0

嗨@Denis Fetinin。它给了我“得到的链接:设置([])”,文件无法下载。有一些错误吗? –

+0

@RahulPipalia,我实际上运行该脚本,它下载文件就好了。你运行的是哪个Python版本?你使用的是什么美丽的版本?我会尝试使用调试器运行脚本,检查其无法分析链接的位置。否则,你可以使用大量的'print'语句来查看发生了什么。 –

+0

@ Denis Fetinin-我正在使用python 2.7.10。我正在使用beautifulsoup4。我会尝试再次运行,看看它是否有效。 –