一个简单的爬虫case2

Posted bernieloveslife

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的爬虫case2相关的知识,希望对你有一定的参考价值。

目标是把这里的ppt什么的给下下来:https://web.stanford.edu/~jurafsky/slp3/

import requests
from bs4 import BeautifulSoup
import os
import socket
import time

socket.setdefaulttimeout(20)
url = ‘https://web.stanford.edu/~jurafsky/slp3/‘
r = requests.get(url)
root = ‘./crawl‘
soup = BeautifulSoup(r.text,"html.parser")
# print(soup)
count = 0
for link in soup.find_all("a"):
    name = link.get(‘href‘)
    if name.split(‘.‘)[-1] in [‘pdf‘,‘ppt‘,‘pptx‘]:
        print(url + name)
        path = root + ‘/‘+ name.split(‘/‘)[-1]
        if not os.path.exists(root):
            os.mkdir(root)
        if not os.path.exists(path):
            r = requests.get(url + name)
            with open(path, "wb") as f:
                f.write(r.content)
                f.close()
        count += 1
        print(count)
        r.close()
        time.sleep(1)
print("finished")

以上是关于一个简单的爬虫case2的主要内容,如果未能解决你的问题,请参考以下文章

scrapy主动退出爬虫的代码片段(python3)

爬虫遇到头疼的验证码?Python实战讲解弹窗处理和验证码识别

根据字符串名称而不是列表视图项位置替换片段并启动活动

简单的方法来分享/讨论/协作的代码片段?

NIH周三讲座视频爬虫

Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段