Python 请求不回调我的函数
如果我的问题太琐碎,我很抱歉,但我从今天早上起就一直躲在墙后面。。。我是新来的,我已经看过医生了,但是我还没有找到我的答案 我编写了这个spider,当我在Python 请求不回调我的函数,python,parsing,web-scraping,scrapy,Python,Parsing,Web Scraping,Scrapy,如果我的问题太琐碎,我很抱歉,但我从今天早上起就一直躲在墙后面。。。我是新来的,我已经看过医生了,但是我还没有找到我的答案 我编写了这个spider,当我在rules=(Rule(LinkExtractor(),callback='parse_body'),)中调用parse_body时,它会: tchatch = response.xpath('//div[@class="ProductPriceBox-item detail"]/div/a/@href').extract()
rules=(Rule(LinkExtractor(),callback='parse_body'),)中调用parse_body
时,它会:
tchatch = response.xpath('//div[@class="ProductPriceBox-item detail"]/div/a/@href').extract()
print('\n TROUVE \n')
print(tchatch)
print('\n DONE \n')
但是,当我在代码中的任何地方重命名函数parse_body
时,它只是执行以下操作:
print('\n EN FAIT, ICI : ', response.url, '\n')
我的scrapy.Request
请求似乎从未被调用过。。。。
我甚至打印了很多无用的东西来知道我的代码是否正在运行函数,但是除了上面写的print
之外,它什么也不打印
有什么想法吗
# -*- coding: utf-8 -*-
import scrapy
import re
import numbers
from fnac.items import FnacItem
from urllib.request import urlopen
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from bs4 import BeautifulSoup
class Fnac(CrawlSpider):
name = 'FnacCom'
allowed_domains = ['fnac.com']
start_urls = ['http://musique.fnac.com/a10484807/The-Cranberries-Something-else-CD-album']
rules = (
Rule(LinkExtractor(), callback='parse_body'),
)
def parse_body(self, response):
item = FnacItem()
nb_sales = response.xpath('//body//table[@summary="données détaillée du vendeur"]/tbody/tr/td/span/text()').re(r'([\d]*) ventes')
country = response.xpath('//body//table[@summary="données détaillée du vendeur"]/tbody/tr/td/text()').re(r'([A-Z].*)')
item['nb_sales'] = ''.join(nb_sales).strip()
item['country'] = ''.join(country).strip()
print(response.url)
test_list = response.xpath('//a/@href')
for test_list in response.xpath('.//div[@class="ProductPriceBox-item detail"]'):
tchatch = response.xpath('//div[@class="ProductPriceBox-item detail"]/div/a/@href').extract()
print('\n TROUVE \n')
print(tchatch)
print('\n DONE \n')
yield scrapy.Request(response.url, callback=self.parse_iframe, meta={'item': item})
def parse_iframe(self, response):
f_item1 = response.meta['item']
print('\n EN FAIT, ICI : ', response.url, '\n')
soup = BeautifulSoup(urlopen(response.url), "lxml")
iframexx = soup.find_all('iframe')
if (len(iframexx) != 0):
for iframe in iframexx:
yield scrapy.Request(iframe.attrs['src'], callback=self.extract_or_loop, meta={'item': f_item1})
else:
yield scrapy.Request(response.url, callback=self.extract_or_loop, meta={'item': f_item1})
def extract_or_loop(self, response):
f_item2 = response.meta['item']
print('\n PEUT ETRE ICI ? \n')
address = response.xpath('//body//div/p/text()').re(r'.*Adresse \: (.*)\n?.*')
email = response.xpath('//body//div/ul/li[contains(text(),"@")]/text()').extract()
name = response.xpath('//body//div/p[@class="customer-policy-label"]/text()').re(r'Infos sur la boutique \: ([a-zA-Z0-9]*\s*)')
phone = response.xpath('//body//div/p/text()').re(r'.*Tél \: ([\d]*)\n?.*')
siret = response.xpath('//body//div/p/text()').re(r'.*Siret \: ([\d]*)\n?.*')
vat = response.xpath('//body//div/text()').re(r'.*TVA \: (.*)')
if (len(name) != 0):
print('\n', name, '\n')
f_item2['name'] = ''.join(name).strip()
f_item2['address'] = ''.join(address).strip()
f_item2['phone'] = ''.join(phone).strip()
f_item2['email'] = ''.join(email).strip()
f_item2['vat'] = ''.join(vat).strip()
f_item2['siret'] = ''.join(siret).strip()
yield f_item2
else:
for sel in response.xpath('//html/body'):
list_urls = sel.xpath('//a/@href').extract()
list_iframe = response.xpath('//div[@class="ProductPriceBox-item detail"]/div/a/@href').extract()
if (len(list_iframe) != 0):
for list_iframe in list_urls:
print('\n', list_iframe, '\n')
print('\n GROS TCHATCH \n')
yield scrapy.Request(list_iframe, callback=self.parse_body)
for url in list_urls:
yield scrapy.Request(response.urljoin(url), callback=self.parse_body)
在爬行爬行器的粗略文档中,有一个警告:
警告
在编写爬行爬行器规则时,避免使用parse
作为回调,因为爬行爬行器使用parse
方法本身来实现其逻辑。因此,如果覆盖parse
方法,爬行爬行器将不再工作
您可以查看此链接,这是谢谢,我将查看链接。。。你对另一个问题有什么想法吗?如果我写了parse_body,我的请求也不起作用。…@P.Postrique我试着调试你的代码,我做的第一件事就是将parse_body
更改为parse_start_url
,这允许调用第二个函数parse_iframe
,但在这个函数中,您正在访问的链接被重定向如下:2017-07-13 12:31:51[scrapy.downloadermiddleware.redirect]调试:重定向(302)到登录页面的起始位置仅仅更改名称,我的函数被调用了吗?我不明白。。。只调用一次parse\u iframe
是正常的吗?不,正如我前面所说的,它的目标不是总是在您想要继承的Scrapy类中使用方法。但这里的重点是重定向到登录页面,似乎某个url受到保护。如果您甚至没有使用真正的规则,为什么不使用Spider
而不是CrawlSpider
?因为如果我将Spider
而不是CrawlSpider
,我有这个错误,可能是因为你没有按原样实现解析方法。我怎么能做到呢?我对scrapy和python了解不多。。。我是否必须将parse_body
更改为parse
?或者要编写另一个名为parse
的函数,请阅读说明: