- 37
- 0
问题描述
在下载http://www.umei.cc/p/gaoqing/...,无法将一个图集放到同一个目录中
问题出现的环境背景及自己尝试过哪些方法
尝试了网上很多方法,无法解决
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
#coding:utf-8
import random
import re
import urllib2
from urllib import urlopen
import requests
import logging
import time
from bs4 import BeautifulSoup,Comment
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from z2.items import Z2Item
from scrapy.http import Request
logging.basicConfig(
level=logging.INFO,
format=
'%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='cataline.log',
filemode='w')
class Spider(CrawlSpider):
name = 'z2'
img_urls = []
allowed_domains = ["www.umei.cc"]
start_urls = ['http://www.umei.cc/p/gaoqing/rihan/']
# rules = (
# Rule(LinkExtractor(allow=('http://www.umei.cc/p/gaoqing/rihan/\d{1,6}.htm',), deny=('http://www.umei.cc/p/gaoqing/rihan/\d{1,6}_\d{1,6}.htm')),
# callback='parse_z2_info', follow=True),
# )
def start_requests(self):
yield Request(url='http://www.umei.cc/p/gaoqing/rihan/',
callback=self.parse_z2_key)
def parse_z2_key(self, response):
soup = BeautifulSoup(response.body, "lxml")
content = soup.find("div", attrs={'class': 'TypeList'})
# logging.debug(content)
for link in content.findAll("a", attrs={'href': re.compile( r'(.*)(/rihan/)(\d{1,6})(.htm)'), 'class': 'TypeBigPics'}):
logging.debug(link['href'])
yield Request(url=link['href'],
callback=self.parse_z2_info)
break
def parse_z2_info(self, response):
soup = BeautifulSoup(response.body, "lxml")
item = Z2Item()
# 去除html注释
for element in soup(text=lambda text: isinstance(text, Comment)):
element.extract()
# 过滤script标签
[s.extract() for s in soup('script')]
# 过滤b标签
[s.extract() for s in soup('b')]
ArticleDesc = soup.find("p", attrs={'class': 'ArticleDesc'})
logging.debug(ArticleDesc.get_text())
Pages = soup.find("div", attrs={'class': 'NewPages'}).find('li')
pageCounts = filter(str.isdigit, Pages.get_text().encode('gbk'))
# 第一种含中文的字符串中提取数字的方法
# logging.debug(re.findall(r"\d+\.?\d*", Pages.get_text())[0])
# 第二种
# logging.debug(Pages.get_text()[1:-3])
# 第三种
logging.debug(filter(str.isdigit, Pages.get_text().encode('gbk')))
# img = soup.find("div", attrs={'class': 'ImageBody'}).find('img')
# url = img.attrs['src']
# self.img_urls.append(url)
# logging.debug(self.img_urls)
item['name'] = re.match(".*/(\d+)", response.url).group(1)
logging.debug(item['name'])
# image_urls = []
# item['image_urls'] = image_urls
sourceUrl = response.url[0:-4]
# logging.debug(sourceUrl)
for i in xrange(1, int(pageCounts) + 1):
nextUrl = sourceUrl + '_' + str(i) + '.htm'
# logging.debug(nextUrl)
yield Request(url=nextUrl,callback=self.parse_z2_single_img)
item['image_urls'] = self.img_urls
yield item
def parse_z2_single_img(self, response):
soup = BeautifulSoup(response.body, "lxml")
img = soup.find("div", attrs={'class': 'ImageBody'}).find('img')
url = img.attrs['src']
self.img_urls.append(url)
你期待的结果是什么?实际看到的错误信息又是什么?
- 共 0 条
- 全部回答
-
十月二十七 普通会员 1楼
Scrapy 是一个用于爬取和分析数据的 Python 框架。在 Scrapy 中,可以使用
Image模块来下载图片并对其进行分类。以下是一个简单的例子:首先,确保已经安装了
requests和BeautifulSoup模块。如果没有,请运行以下命令安装:bash pip install requests beautifulsoup4接下来,创建一个 Scrapy 应用并添加一个
Scrapy爬虫项目:bash scrapy startproject image_recognition cd image_recognition然后,在项目中创建一个
settings.py文件,以便指定爬虫的网络配置:```python import scrapy
class ImageRecognitionSpider(scrapy.Spider): name = 'image_recognition' allowed_domains = ['example.com'] start_urls = ['http://example.com']
def parse(self, response): for image in response.css('img'): yield { 'image_url': image['src'], 'category': image.css('h1::text').get(), }```
在这个例子中,
http://example.com是网站的 URL,而h1::text是一个 CSS 样式选择器,用于获取页面上的所有标题文本。这将返回一个字典,包含每个图片的 URL 和它的分类。要运行这个爬虫,打开终端或命令提示符,并导航到项目的目录。然后运行以下命令:
bash scrapy crawl image_recognition这将启动 Scrapy 爬虫,并开始抓取指定网站上的所有图片。爬虫将使用
Image模块下载图片,并使用 CSS 样式选择器来分类图片。
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
