首页 > 代码库 > 爬取糗事百科的图片

爬取糗事百科的图片

小编,最近写了个单线程的爬虫,主要是爬取糗事百科的图片之类的,下面是源代码,小伙伴们可以拿去参照,学习


#!/usr/bin/env python

# -*- coding:utf-8 -*-
import requests,json
import requests,re,os,time
import urllib.request
import urllib.parse
import ssl
import unittest
from selenium import webdriver

headers = {"User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"}
os.chdir("meizhu")
def fc(url):

req = urllib.request.Request(url, headers = headers)
html = urllib.request.urlopen(req).read().decode(‘utf-8‘)

r =‘<img src="http://(.+?\.jpg)" alt="[\u4e00-\u9fa5]{1,}"/>‘
result = re.compile(r).findall(str(html))

h=(list(result))
print(h)

for img in h:
imgurl ="http://" + img
imaname = str(img.split(‘/‘)[4]+".jpg")
file = urllib.request.urlretrieve(str(imgurl),filename=imaname)

for i in range(1,5):
url = "http://www.qiushibaike.com/pic/page/" + str(i)
fc(url)

爬取糗事百科的图片