这是我的pipelines.py(python3 + scrapy1.4)。为什么pipelines.py无法保存从网页抓取的内容?
import urllib.request
class MoviePipeline(object):
def process_item(self, item, spider):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'}
req = urllib.request.Request(url=item['addr'],headers=headers)
res = urllib.request.urlopen(req)
file_name = '/tmp/'+item['name']+'.jpg'
print(file_name)
with open(file_name,'wb') as fp:
fp.write(res.read())
1.打印(FILE_NAME)不能工作
打印(项目[ '名'])可打印item'name我movie.py的解析功能。
为什么print(item ['name'])不能在pipelines.py中使用scrapy抓取电影执行我的蜘蛛?
2.为何
import urllib.request
addr = 'selected_from_crawled_url'
req = urllib.request.Request(url= addr)
res = urllib.request.urlopen(req)
file_name = "/tmp/test.jpg"
with open(file_name,'wb') as fp:
fp.write(res.read())
这验证了上面的代码片段工作正常,没有JPG文件保存在/ tmp目录下,为什么同样的结构,管道不能工作?
你验证'process_item'你'MoviePipeline'被甚至被称为?你在'settings.py'中添加了那条管道吗? –