python结合BeautifulSoup爬虫小实战

Python结合BeautifulSoup爬虫小实战,对虎扑不冷笑话进行GIF图片获取。文章内容仅作学习之用。

import urllib.request
import os
from bs4 import BeautifulSoup

try:
    siteURL = 'https://my.hupu.com/search?fid=34&type=topic&q=%E3%80%8A%E4%B8%8D%E5%86%B7%E7%AC%91%E8%AF%9D%E3%80%8B%E7%AC%AC'
    start_html = urllib.request.urlopen(siteURL).read().decode('utf8')
    Soup = BeautifulSoup(start_html, 'html.parser')
    td_list = Soup.find_all('td', class_='p_title')
    for x in td_list:
        link = x.a['href']
        text = x.get_text()
        html = urllib.request.urlopen(link).read().decode('utf8')
        html_soup = BeautifulSoup(html, 'html.parser')
        name = html_soup.find('div', class_="subhead").span.get_text()
        path = os.path.join("D:\\buleng", name)
        if os.path.exists(path):
            continue
        else:
            os.mkdir(path)
            os.chdir(path)

        a_list = html_soup.find('div', class_="floor_box").find_all('img')
        i = 1
        for list in a_list:
            urllib.request.urlretrieve(list['src'], '{0}.gif'.format(i))
            i += 1
        print(name + '---- 下载完成')
    print('--------全部下载完成--------')
except urllib.error.URLError as e:
    print(e.reason)


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值