码迷,mamicode.com
首页 > Web开发 > 详细

爬虫遇到HTTP Error 403的问题

时间:2019-06-03 22:00:21      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:get   parser   exception   coding   txt   requests   write   lib   sel   

# coding=utf-8


from bs4 import BeautifulSoup
import requests
import urllib
x = 1
y = 1

def crawl(url):
    res = requests.get(url)
    soup = BeautifulSoup(res.text, 'html.parser')
    global y
    with open(f'C:/Users/Administrator/Desktop/alien/pachong/xnt/{y}.txt','w',encoding="utf-8") as f:
        f.write(str(soup))
        y += 1
    yinhuns = soup.select('img')
    print()
    for yh in yinhuns:
        print(yh)
        link = yh.get('src')
        print(link)
        global x    
        urllib.request.urlretrieve(link, f'C:/Users/Administrator/Desktop/alien/pachong/xnt/{x}.jpg')
        print(f'正在下载第{x}张图片')
        x += 1
        
for i in range(1,5):
    url = "https://acg.fi/hentai/23643.htm/" + str(i)
    
    try:
        crawl(url)
    except ValueError as f:
        continue
    except Exception as e:
        print(e)

爬虫遇到HTTP Error 403的问题

标签:get   parser   exception   coding   txt   requests   write   lib   sel   

原文地址:https://www.cnblogs.com/rener0424/p/10970096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!